RxJava operator (3) Filtering. RxJava operator (3) Filtering in the previous article, we learned about conversion operators and can convert data into the desired format, however, if there are some data sets that we want to RxJava operator (3) Filtering
In the previous article, we learned about conversion operators that can convert data into the desired format. But what if there is some data in the data set that we want to filter out? At this time, we need to use the filter operator, which is a bit similar to the where in SQL, so that the Observable only returns data that meets our conditions.
I. debounce
The debounce operator is used to throttling. it can be understood as a valve. when you open the valve halfway, the water will flow out at a slow speed. The difference is that the water in the valve will not be wasted, and the data filtered out by debounce will be discarded. In Rxjava, the operator atmosphere is changed.
throttleWithTimeoutAnd
Debounce operator. Let's take a look at throttleWithTimeOut, as shown in. this operator throws traffic through time. the Source Observable will start timing each time it emits a data, if a new data is sent from the source Observable before the set time ends, the data is discarded and the timing starts again. If data is transmitted before the end of each time, the throttling will go to the extreme: only the last data will be transmitted.
First, we will create an Observable to send data every 100 milliseconds. when the data to be transmitted is a multiple of 3, the next data will be delayed to 300 milliseconds before being transmitted.
- private Observable
createObserver() {
- return Observable.create(new Observable.OnSubscribe
() {
- @Override
- public void call(Subscriber
subscriber) {
- for (int i = 0; i < 10; i++) {
- if (!subscriber.isUnsubscribed()) {
- subscriber.onNext(i);
- }
- int sleep = 100;
- if (i % 3 == 0) {
- sleep = 300;
- }
- try {
- Thread.sleep(sleep);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- subscriber.onCompleted();
- }
- }).subscribeOn(Schedulers.computation());
- }
The following uses throttleWithTimeOut to filter out the Observable. we set the filtering time to 200 milliseconds. that is to say, data with an interval less than 200 milliseconds will be filtered out.
- private Observable
throttleWithTimeoutObserver() {
- return createObserver().throttleWithTimeout(200, TimeUnit.MILLISECONDS)
- .observeOn(AndroidSchedulers.mainThread());
- }
Subscribe to it
- mLButton.setText("throttleWithTimeout");
- mLButton.setOnClickListener(e -> throttleWithTimeoutObserver().subscribe(i -> log("throttleWithTimeout:" + i)));
The running result is as follows. we can see that new data is generated within 200 milliseconds after the data is not a multiple of 3, so it is filtered out.
The debounce operator can also use time for filtering. in this case, it is used in the same way as throttleWithTimeOut, but the deounce operator can also throttling based on a function. The returned value of this function is a temporary Observable. if the source Observable transmits a new data, the previous data is not completed yet according to the temporary Observable generated by the function, the previous data will be filtered out.
Generate an Observable and use debounce to filter it. the onCompleted method is called only when the transmitted data is an even number to indicate that the temporary Observable has been terminated.
- private Observable
debounceObserver() {
- return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).debounce(integer -> {
- log(integer);
- return Observable.create(new Observable.OnSubscribe
() {
- @Override
- public void call(Subscriber
subscriber) {
- if (integer % 2 == 0 && !subscriber.isUnsubscribed()) {
- log("complete:" + integer);
- subscriber.onNext(integer);
- subscriber.onCompleted();
- }
- }
- });
- })
- .observeOn(AndroidSchedulers.mainThread());
- }
Subscribe to it
- mRButton.setOnClickListener(e -> debounceObserver().subscribe(i -> log("debounce:" + i)));
The running result is as follows. we can see that only the data that calls the onCompleted method will be sent out, and the other data will be filtered out.
II. Distinct
The Distinct operator is used to remove duplicates, which is easy to understand. As shown in, all duplicate data will be filtered out. Another operator, distinctUntilChanged, is used to filter out consecutive duplicate data.
Create two Observable instances and filter them by using the Distinct and DistinctUtilChanged operators.
- private Observable
distinctObserver() {
- return Observable.just(1, 2, 3, 4, 5, 4, 3, 2, 1).distinct();
- }
- private Observable
distinctUntilChangedObserver() {
- return Observable.just(1, 2, 3, 3, 3, 1, 2, 3, 3).distinctUntilChanged();
- }
Subscribe
- mLButton.setText("distinct");
- mLButton.setOnClickListener(e -> distinctObserver().subscribe(i -> log("distinct:" + i)));
- mRButton.setText("UntilChanged");
- mRButton.setOnClickListener(e -> distinctUntilChangedObserver().subscribe(i -> log("UntilChanged:" + i)));
The running result is as follows. we can see that Distinct filters out all duplicate numbers, and DistinctUtilChanged filters out duplicate numbers only.
3. ElementAt and Filter
These two operators are well understood. ElementAt only returns data at the specified position, while Filter only returns data that meets the filtering conditions, as shown below:
Create two Observable objects and use the ElementAt and Filter operators to Filter them.
- private Observable
elementAtObserver() {
- return Observable.just(0, 1, 2, 3, 4, 5).elementAt(2);
- }
- private Observable
FilterObserver() {
- return Observable.just(0, 1, 2, 3, 4, 5).filter(i -> i < 3);
- }
Subscribe to them separately
- mLButton.setText("elementAt");
- mLButton.setOnClickListener(e -> elementAtObserver().subscribe(i -> log("elementAt:" + i)));
- mRButton.setText("Filter");
- mRButton.setOnClickListener(e -> FilterObserver().subscribe(i -> log("Filter:" + i)));
The running result is as follows:
4. First and Last
The First operator returns only the First data and the First data that meets the conditions. If you have read my previous blog, you will find that in our example of using Rxjava to implement a third-level cache, we use the first operator to select the cache to be used. In contrast to the First operator, the Last operator returns only the data that meets the conditions.
There is also a BlockingObservable method, which does not process Observable, but blocks it. when data that meets the conditions is sent out, a BlockingObservable object is returned. AvailableObservable.toBlockingOrBlockingObservable.fromMethod to convert an Observable object to a BlockingObservable object. BlockingObservable can be used with the first operator.
Create two Observable objects and use the first operator for processing.
- private Observable
FirstObserver() {
- return Observable.just(0, 1, 2, 3, 4, 5).first(i -> i > 1);
- }
- private BlockingObservable
FilterObserver() {
- return Observable.create(new Observable.OnSubscribe
() {
- @Override
- public void call(Subscriber
subscriber) {
- for (int i = 0; i < 5; i++) {
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (!subscriber.isUnsubscribed()) {
- log("onNext:" + i);
- subscriber.onNext(i);
- }
- }
- if (!subscriber.isUnsubscribed()) {
- subscriber.onCompleted();
- }
- }
- }).toBlocking();
- }
Subscribe separately
- mLButton.setText("First");
- mLButton.setOnClickListener(e -> FirstObserver().subscribe(i -> log("First:" + i)));
- mRButton.setText(" Blocking");
- mRButton.setOnClickListener(e -> {
- log("blocking:" + FilterObserver().first(i -> i > 1));
- });
The running result is as follows. We can see that the first operator returns the first number 2 greater than 1, while BlockingObservable blocks until the first data greater than 1 is released.
V. Skip and Take
The Skip operator filters out the first n items of data transmitted by the source Observable, while the Take operator takes only the first n items. it is easy to understand and use, but it is very useful. In addition, there are SkipLast and TakeLast operators, respectively, from the back of the filter operation.
Create two Observable instances and use the skip and take operators to filter them.
- private Observable
skipObserver() {
- return Observable.just(0, 1, 2, 3, 4, 5).skip(2);
- }
- private Observable
takeObserver() {
- return Observable.just(0, 1, 2, 3, 4, 5).take(2);
- }
Subscribe separately
- mLButton.setText("Skip");
- mLButton.setOnClickListener(e -> skipObserver().subscribe(i -> log("Skip:" + i)));
- mRButton.setText("Take");
- mRButton.setOnClickListener(e -> takeObserver().subscribe(i -> log("Take:" + i)));
The running result is as follows. you can see that skip filters out the first two items, while take filters out all the items except the first two items.
6. Sample and ThrottleFirst
The Sample operator periodically transmits the data recently released by the source Observable. other data will be filtered out, which is equivalent to the ThrottleLast operator. the ThrottleFirst operator periodically transmits the first data transmitted by the source Observable during this time period.
We create an Observable to send a data every 200 milliseconds, and then use the sample and throttleFirst operators to filter the data.
- private Observable
sampleObserver() {
- return createObserver().sample(1000, TimeUnit.MILLISECONDS);
- }
- private Observable
throttleFirstObserver() {
- return createObserver().throttleFirst(1000, TimeUnit.MILLISECONDS);
- }
- private Observable
createObserver() {
- return Observable.create(new Observable.OnSubscribe
() {
- @Override
- public void call(Subscriber
subscriber) {
- for (int i = 0; i < 20; i++) {
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- subscriber.onNext(i);
- }
- subscriber.onCompleted();
- }
- });
- }
Subscribe separately
- mLButton.setText("sample");
- mLButton.setOnClickListener(e -> sampleObserver().subscribe(i -> log("sample:" + i)));
- mRButton.setText("throttleFirst");
- mRButton.setOnClickListener(e -> throttleFirstObserver().subscribe(i -> log("throttleFirst:" + i)));
The running result is as follows. we can see that the sample operator emits a data packet every five digits, while the throttleFirst operator emits the first data packet every five digits.
The demo program of this article see github: https://github.com/Chaoba/RxJavaDemo
Embedding (3) Filtering in the previous article, we learned about conversion operators and can convert data into the desired format. However, if there is something in the data set that we want...