Filtering of RxJava operator Summary
RxJava operator Summary-filtering jsut ()
just(T t1, T t2, T t3 ....)
, Just can pass in multiple parameters of the same type, and send the current parameter one by one.
Observable.just("1","2","3") .subscribe(new Action1
() { @Override public void call(String s) { System.out.println(s); } });
123
Repeat ()
repeat()
Sends the current message sequence in an unrestricted loop. We can input a parameter to indicate the number of cycles.
Observable.just("1","2","3") .repeat(3) .subscribe(new Action1
() { @Override public void call(String s) { System.out.println(s); } });
123123123
Defer ()
Delayed CreationObservable
. Create a subscriptionObservable
Object. Use this methodcall
Method features.
public static void main(String[] args) { Observable.defer(new Func0
>() { @Override public Observable
call() { return getInt(); } }); } public static Observable
getInt() { System.out.println("getInt()"); return Observable.create(new Observable.OnSubscribe
() { @Override public void call(Subscriber subscriber) { System.out.print("ss"); subscriber.onNext(42); } }); }
In this casegetInt()
The method will not be called.subscribe()
. It is easy to understand the source code.
Range ()
Starts from the specified number.
Observable.range(3,2) .subscribe(new Action1
() { @Override public void call(Integer integer) { System.out.print(integer); } });
34
range(int start,int count)
The first parameter is the starting number and the second parameter is the number of sent messages.
Filter ()
Filter. Filters the Sequence Based on the callback conditions.
Query 0 ~ 49 Number of divisible by 3.
Private static ArrayList Array = new ArrayList <> ();
Public static void main (String [] args ){
Init ();
Observable. from (array)
. Filter (new Func1 (){
@ Override
Public Boolean call (Integer integer ){
// Judgment condition. If false is returned, the sent content will be canceled and true will be sent again.
Return integer % 3 = 0;
}
})
. Subscribe (new Action1 (){
@ Override
Public void call (Integer integer ){
System. out. print (integer + "");
}
});
}
Public static void init (){
For (int I = 0; I <50; I ++ ){
Array. add (I );
}
}
0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48
Take () takeLast ()
take()
Get the first few of the emission sequence, and cancel sending later.
takeLast()
Get the last few transmit sequences, and cancel the rest before sending them down.
Obtain 0 ~ Number of the first three and last three in 49
Observable.from(array) .take(3) .subscribe(new Action1
() { @Override public void call(Integer integer) { System.out.print(integer+" "); } }); System.out.println(); Observable.from(array) .takeLast(3) .subscribe(new Action1
() { @Override public void call(Integer integer) { System.out.print(integer+" "); } });
0 1 2 47 48 49
Distinct ()
Removes duplicate values from the sending sequence. That is, if the value after the sending sequence overlaps with the previous one, the subsequent value will not be sent. This method records the value of each sending sequence when removing duplicates, so pay attention to the sent value when there is large data.
Observable.from(array) .take(3) .repeat(3) .distinct() .subscribe(new Action1
() { @Override public void call(Integer integer) { System.out.print(integer+" "); } });
0 1 2
DistinctUntilChanged ()
This method anddistinct()
The difference is that if the current launch value is the same as the previous launch value, the current launch will be canceled. If the current launch value is different, the launch will continue. That is, launch when there is a change.
First () and last ()
Therefore, the first and last pieces of the emission sequence are obtained.
At the same time, this method can select the first and last matching conditions according to the conditions.
Obtain 0 ~ The last value in multiples of 3 in 49
Observable.from(array) .last(new Func1
() { @Override public Boolean call(Integer integer) { return integer%3==0&&integer!=0; } }) .subscribe(new Action1
() { @Override public void call(Integer integer) { System.out.print(integer+" "); } });
48
Skip () and skipLast ()
Skip the first and last few emission sequences. This method andtake()
,takeLast()
Similar.
Skip 0 ~ 49 the first three and the last three in the emission sequence
Observable.from(array) .skip(3) .skipLast(3) .subscribe(new Action1
() { @Override public void call(Integer integer) { System.out.print(integer+" "); } });
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
ElementAt () and elementAtOrDefault ()
Obtains the emission value at the specified position of the emission sequence. When the specified position is greater than the emission sequence, an exception is thrown.elementAtDefault()
.
0 ~ 49. Obtain the first three elements and then obtain the element value at the fifth position. If not, set the default value to 3.
Observable.from(array) .take(3) .elementAtOrDefault(5,3) .subscribe(new Action1
() { @Override public void call(Integer integer) { System.out.print(integer+" "); } });
3
Interval ()
Round robin: This operator sends an event at a specified time. By defaultconmputation
Thread execution
Observable .interval(3, TimeUnit.SECONDS) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1
() { @Override public void call(Long s) { Log.i("info",s); } } );
First parameter: delay time second parameter: Unit
This operator starts from 0 and is sent once every 1 second.
The Code is as follows:
Observable.interval(3,TimeUnit.SECONDS) .flatMap(new Func1
>() { @Override public Observable
call(Long aLong) { return Observable.just(array.get(aLong.intValue())); } }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber
() { @Override public void onCompleted() { Log.i("info","onCompleted"); } @Override public void onError(Throwable e) { Log.i("info","error"); } @Override public void onNext(String s) { Log.i("info","onNext--"+s); } });
04-13 15:44:28.634 15455-15455/mahao.alex.rxjava I/info: onNext--aa04-13 15:44:31.634 15455-15455/mahao.alex.rxjava I/info: onNext--bb04-13 15:44:34.634 15455-15455/mahao.alex.rxjava I/info: onNext--cc04-13 15:44:37.634 15455-15455/mahao.alex.rxjava I/info: onNext--dd04-13 15:44:40.644 15455-15455/mahao.alex.rxjava I/info: error
Print the following information every three seconds. Print once ..
Timer ()
The sending element after a fixed delay time. Andinterval()
The difference is that this operator is only sent once.
Observable.timer(3,TimeUnit.SECONDS) .flatMap(new Func1
>() { @Override public Observable
call(Long aLong) { return Observable.just(array.get(aLong.intValue())); } }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber
() { @Override public void onCompleted() { Log.i("info","onCompleted"); } @Override public void onError(Throwable e) { Log.i("info","error"); } @Override public void onNext(String s) { Log.i("info","onNext--"+s); } });
04-13 15:52:32.114 23036-23036/mahao.alex.rxjava I/info: onNext--aa04-13 15:52:32.114 23036-23036/mahao.alex.rxjava I/info: onCompleted
Note: This operator runs onconputation
Thread.
Sample ()
Obtain the latest value of the emission sequence at a fixed interval and send it down.
There are two cases.
The time interval of the sending sequence is greater
sample
The time interval of the sending sequence is less
sample
Time Interval
In the second case, it is to launch at a fixed interval, while in the first case, there is a special situation.
Let's take a look at the example below
There is an array {"aa", "bb", "cc", "dd"} which is sent every three seconds, whilesample
Filter every two seconds.
Observable.interval(3,TimeUnit.SECONDS) .flatMap(new Func1
>() { @Override public Observable
call(Long aLong) { return Observable.just(array.get(aLong.intValue())); } }) .sample(2,TimeUnit.SECONDS) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber
() { @Override public void onCompleted() { Log.i("info","onCompleted"); } @Override public void onError(Throwable e) { Log.i("info","error"); } @Override public void onNext(String s) { Log.i("info","onNext--"+s); } });
04-13 16:13:45.404 11935-11935/mahao.alex.rxjava I/info: onNext--aa04-13 16:13:49.404 11935-11935/mahao.alex.rxjava I/info: onNext--bb04-13 16:13:51.404 11935-11935/mahao.alex.rxjava I/info: onNext--cc04-13 16:13:55.404 11935-11935/mahao.alex.rxjava I/info: onNext--dd04-13 16:13:56.414 11935-11935/mahao.alex.rxjava I/info: error
Let's take a look at their event interval. The interval between four launches is 4, 2, and 4. The last error is not mentioned.
Why is this time interval?
Although the figure is ugly, it still makes sense.
Next, let's look at the image.
TimeOut
Specify the minimum interval for transmitting data. If no sending element exists during the specified interval, an exception is thrown and the task is stopped.
Debounce
When the time interval for sending data is lessdebounce
If no data is sent within the specified time interval, the last data is sent.