In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the RxJava operators". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the RxJava operators are.
Filter
This operator receives a Func1 parameter, in which we can judge the data we want to filter by using your own judgment condition, and when the data passes the judgment condition, it returns true to indicate that the data is transmitted, otherwise it will not be emitted, thus filtering out the data we want. As follows, we filter out the numbers that are not divisible by 2
Integer [] ints = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Observable observable = Observable.from (ints) .filter (new Func1 () {@ Override public Boolean call (Integer integer) {return integer% 2! = 0 true / return true, it will not be filtered, the data will be transmitted, and the value returned by filter}}) Action1 action1 = new Action1 () {@ Override public void call (Integer I) {Log.e (TAG, "call:" + I);}}; observable.subscribe (action1)
Output log information
Call: 1 call: 3 call: 5 call: 7 call: 9
OfType
This operator is a special form of the filter operator. It filters an Observable that only returns data of a specified type. For example, when the data source has a string and int data, we can use this operator if we want to filter out the string, as shown in the following sample code
Observable.just (0, "one", 6, 4, "two", 8, "three", 1, "four", 0) .ofType (String.class) .subscribe (new Subscriber () {@ Override public void onCompleted () {Log.e (TAG, "onCompleted:ofType");} @ Override public void onError (Throwable e) {Log.e (TAG, "onError:ofType") @ Override public void onNext (String string) {Log.e (TAG, "onNext:ofType" + string);}})
Output log information
OnNext:ofType one onNext:ofType two onNext:ofType three onNext:ofType four onCompleted:ofType
Of course, in addition to filtering basic type data, you can also filter custom type data.
First
If we are only interested in the * item data emitted by Observable, or if the * item data meets a certain condition, we can use the First operator.
Observable.just (10, 11, 12, 13). First (). Subscribe (new Action1 () {@ Override public void call (Integer integer) {Log.e (TAG, integer+ ");}})
The above log only prints a value of 10. Of course, we can also pass a parameter Fun1 to first. Specify a condition as follows.
Observable.just (10,11,12,13) .first (new Func1 () {@ Override public Boolean call (Integer integer) {return integer > 12;}}) .subscribe (new Action1 () {@ Override public void call (Integer integer) {Log.e (TAG, integer+ "")) })
The output information at this time is the * * item data 13 that satisfies integer > 12.
FirstOrDefault
This operator is a deformation of the first operator. The main thing is to emit a default value that you specify in the parameters when no data is transmitted. As follows, it has two overloaded methods.
Observable.just (11J 12513) .firstOrDefault (10) .subscribe (new Action1 () {@ Override public void call (Object o) {Log.e (TAG, o.toString ());}})
If you write the above code, this execution will have the same effect as first. Since the default value is only used when no data is emitted, we will change the above code as follows and use empty to create an Observable that does not emit any data but terminates normally.
Observable.empty () .firstOrDefault (10) .subscribe (new Action1 () {@ Override public void call (Object o) {Log.e (TAG, o.toString ());}})
It is found that data 10. 5 is output at this time. The operator also provides an overloaded method firstOrDefault (T defaultValue, Func1 super T, Boolean > predicate) with two parameters. We can add a condition. The following example
Observable.just (10Jet 13je 16) .firstOrDefault (15, new Func1 () {@ Override public Boolean call (Integer integer) {return integer > 20;}}) .subscribe (new Action1 () {@ Override public void call (Integer integer) {Log.e (TAG, "" + integer)) })
At this point, if none of the data sources are more than 20, then the default value of 15 will be output, if we increase the data source data by a value of 22. At this point, the default value will no longer be output, but 22.
TakeFirst
The difference between this operator and the first operator is that if the original Observable does not emit any data that meets the criteria, first throws a NoSuchElementException and directly executes onError (), while takeFist returns an empty Observable (does not call onNext () but calls onCompleted).
The following example code is shown
Observable.just (10 Override public void onCompleted 11) .filter (new Func1 () {@ Override public Boolean call (Integer integer) {return integer > 20;}}) .first () .subscribe (new Subscriber () {@ Override public void onCompleted () {Log.e (TAG, "onCompleted:")) } @ Override public void onError (Throwable e) {Log.e (TAG, "onError:" + e.toString ());} @ Override public void onNext (Object o) {Log.e (TAG, "onNext:" + o.toString ()) })
The output information after execution is as follows
OnError: java.util.NoSuchElementException: Sequence contains no elements
If you use takeFirst at this time
Observable.just (10 return integer 11). Creative first (new Func1 () {@ Override public Boolean call (Integer integer) {Log.e (TAG, "call: takeFirst"); return integer > 30 ) .subscribe (new Subscriber () {@ Override public void onCompleted () {Log.e (TAG, "onCompleted:") } @ Override public void onError (Throwable e) {Log.e (TAG, "onError:" + e.toString ());} @ Override public void onNext (Object o) {Log.e (TAG, "onNext:" + o.toString ()) })
It is found that no exception occurs at this time, but onCompleted () is executed.
Single
If the original Observable does not send data just once before it is finished, it will throw a NoSuchElementException. In vernacular, it can be understood as outputting the value of this item if the sent data is one item, and executing the onError () method if there is more than one data.
The code is as follows
Observable.just (10,11,12,13). Single (). Subscribe (new Subscriber () {@ Override public void onCompleted () {Log.e (TAG, "onCompleted");} @ Override public void onError (Throwable e) {Log.e (TAG, "onError" + e.toString ()) } @ Override public void onNext (Integer integer) {Log.e (TAG, integer);}})
Output information
OnError: java.util.NoSuchElementException: Sequence contains no elements
If you make a simple change to the above code
Observable.just (10,11,12,13) .filter (new Func1 () {@ Override public Boolean call (Integer integer) {return integer > 12;}}) .subscribe (new Subscriber () {@ Override public void onCompleted () {Log.e (TAG, "onCompleted") @ Override public void onError (Throwable e) {Log.e (TAG, "onError" + e.toString ());} @ Override public void onNext (Integer integer) {Log.e (TAG, integer);}})
At this point, data 13 is output because there is only one piece of data after passing through the filter. Single also has two variants, singleOrDefault (T) and singleOrDefault (Tforce function 1), which can be tested by your own code.
Last
This operator is the opposite of first and is used if we are interested in only one item of data emitted by Observable, or if we are interested in one item of data that meets a certain condition.
Sample code
Observable.just (10, 11, 12, 13). Last (). Subscribe (new Action1 () {@ Override public void call (Integer integer) {Log.e (TAG, "call:" + integer);}})
Output 13. 5% after execution. It has an overloaded method that can specify a condition to get a piece of data that meets the condition. Modify the above code as follows
Observable.just (10, 11, 12, 13) .last (new Func1 () {@ Override public Boolean call (Integer integer) {return integer
< 12; } }).subscribe(new Action1() { @Override public void call(Integer integer) { Log.e(TAG, "call: "+integer); } }); 此时最终输出数据就是11.该操作符和first一样也有几种变体,如lastOrDefault,TakeLast,具体效果可自己测试。 Skip 该操作符是跳过之前的前几项数据,然后再发射数据。 Observable.range(1, 10).skip(6).subscribe(new Action1() { @Override public void call(Integer integer) { Log.e(TAG, "call: "+integer ); } }); 输出日志信息 call: 7 call: 8 call: 9 call: 10 skip还有两个重载方法.skip(long time, TimeUnit unit)默认是在computation调度器上执行,如果要有更新UI操作需要通过observeOn方法指定为AndroidSchedulers.mainThread(),当然还有一个重载方法skip(long time, TimeUnit unit, Scheduler scheduler)可以指定调度器。注意的一点是这两个重载方法的***个参数不是跳过的数据数量,指的是时间。 Observable.interval(500, TimeUnit.MILLISECONDS) .skip(2, TimeUnit.SECONDS) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Long aLong) { tv.append("\n" + aLong); if (aLong >10) {this.unsubscribe ();}})
As in the above code, a data is generated every 500ms by interval, and the skip time is set to 2 seconds by skip. And unsubscribe when the data is greater than 10:00.
SkipLast
Contrary to skip, ignore the n data items generated by *.
Observable.range (1,10) .skipLast (6) .subscribe (new Action1 () {@ Override public void call (Integer integer) {Log.e (TAG, "call:" + integer);}})
Output log information
Call: 1 call: 2 call: 3 call: 4 Thank you for reading, the above is the content of "what are the RxJava operators?" after the study of this article, I believe you have a deeper understanding of what the RxJava operators have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.