RxJava Operator Series 3 (Part 2)

RxJava Operator Series 3 (Part 2)

[[180593]]

Continued from previous article

Take

The Take operator modifies the behavior of an Observable to return only the first N items of data, then emit a completion notification and ignore the remaining data.

  1. Observable.range(1,8)
  2.  
  3. .take(4)
  4.  
  5. .subscribe(new Subscriber <Integer> () {
  6.  
  7. @Override
  8.  
  9. public void onNext( Integer item) {
  10.  
  11. Log.e(TAG, "Next: " + item);
  12.  
  13. }
  14.  
  15.   
  16.  
  17. @Override
  18.  
  19. public void onError(Throwable error) {
  20.  
  21. Log.e(TAG, "Error: " + error.getMessage());
  22.  
  23. }
  24.  
  25.   
  26.  
  27. @Override
  28.  
  29. public void onCompleted() {
  30.  
  31. Log.e(TAG, "complete." );
  32.  
  33. }
  34.  
  35. });

Output log information

  1. Next : 1
  2.  
  3. Next : 2
  4.  
  5. Next : 3
  6.  
  7. Next : 4
  8.  
  9. complete

Like skip, take also has two other overloaded methods: take(long time, TimeUnit unit) and take(long time, TimeUnit unit, Scheduler scheduler), which are executed on the computation scheduler by default.

take also has a variant operator TakeLast, takeLastBuffer. The specific execution effect can be coded by yourself.

Debounce

This operator means that a data is emitted only when no data is emitted after a specified period of time. It may sound a bit confusing. You can understand it as filtering the results generated by the source Observable interval. If no other results are generated within this specified interval, the result is submitted to the subscriber, otherwise the result is ignored. The principle is a bit like optical image stabilization.

On the code

  1. Observable.range(1,8)
  2.  
  3. .take(4)
  4.  
  5. .subscribe(new Subscriber <Integer> () {
  6.  
  7. @Override
  8.  
  9. public void onNext( Integer item) {
  10.  
  11. Log.e(TAG, "Next: " + item);
  12.  
  13. }
  14.  
  15.   
  16.  
  17. @Override
  18.  
  19. public void onError(Throwable error) {
  20.  
  21. Log.e(TAG, "Error: " + error.getMessage());
  22.  
  23. }
  24.  
  25.   
  26.  
  27. @Override
  28.  
  29. public void onCompleted() {
  30.  
  31. Log.e(TAG, "complete." );
  32.  
  33. }
  34.  
  35. });

Output information

  1. onNext: 4
  2.  
  3. onNext: 5
  4.  
  5. onNext: 6
  6.  
  7. onNext: 7
  8.  
  9. onNext: 8
  10.  
  11. onNext: 9
  12.  
  13. onCompleted:

The output data may not be the same, it may start from 5.

Distinct

This is relatively easy to understand. It filters out duplicate data and only allows data items that have not yet been emitted to pass.

Sample Code

  1. Observable.just(0, 0, 6, 4, 2, 8, 2, 1, 9, 0)
  2.  
  3. .distinct ()
  4.  
  5. .subscribe(new Subscriber <Integer> () {
  6.  
  7. @Override
  8.  
  9. public void onCompleted() {
  10.  
  11. Log.e(TAG, "onCompleted:Distinct " );
  12.  
  13. }
  14.  
  15.   
  16.  
  17. @Override
  18.  
  19. public void onError(Throwable e) {
  20.  
  21. Log.e(TAG, "onError:Distinct " );
  22.  
  23. }
  24.  
  25.   
  26.  
  27. @Override
  28.  
  29. public void onNext( Integer   integer ) {
  30.  
  31. Log.e(TAG, "onNext:Distinct " + integer );
  32.  
  33. }
  34.  
  35. });

Output log information

  1. onNext: Distinct 0
  2.  
  3. onNext: Distinct 6
  4.  
  5. onNext: Distinct 4
  6.  
  7. onNext: Distinct 2
  8.  
  9. onNext: Distinct 8
  10.  
  11. onNext: Distinct 1
  12.  
  13. onNext: Distinct 9
  14.  
  15. onCompleted: Distinct  

ElementAt

This operator gets the data item at the specified index position of the data sequence emitted by the original Observable, and then emits it as its own first data. Pass it a 0-based index value, and it will emit the value of the corresponding index position of the original Observable data sequence. If the value you pass to elementAt is 4, it will emit the data of the 5th item. The following example code

  1. Observable.just(0, 0, 6, 4, 2, 8, 2, 1, 9, 0)
  2.  
  3. .elementAt(4)
  4.  
  5. .subscribe(new Subscriber <Integer> () {
  6.  
  7. @Override
  8.  
  9. public void onCompleted() {
  10.  
  11. Log.e(TAG, "onCompleted:ElementAt " );
  12.  
  13. }
  14.  
  15.   
  16.  
  17. @Override
  18.  
  19. public void onError(Throwable e) {
  20.  
  21. Log.e(TAG, "onError:ElementAt " );
  22.  
  23. }
  24.  
  25.   
  26.  
  27. @Override
  28.  
  29. public void onNext( Integer   integer ) {
  30.  
  31. Log.e(TAG, "onNext:ElementAt " + integer );
  32.  
  33. }
  34.  
  35. });

Output log information

  1. onNext:ElementAt 2
  2.  
  3. onCompleted:ElementAt

IgnoreElements

The operator suppresses all data emitted by the original Observable and only allows its termination notification (onError or onCompleted) to pass through. The onNext() method will not be executed using this operator.

  1. Observable.just(1, 2, 3).ignoreElements().subscribe(new Subscriber() {
  2.  
  3. @Override
  4.  
  5. public void onCompleted() {
  6.  
  7. Log.e(TAG, "onCompleted" );
  8.  
  9. }
  10.  
  11.  
  12.  
  13. @Override
  14.  
  15. public void onError(Throwable e) {
  16.  
  17. Log.e(TAG, "onError" );
  18.  
  19. }
  20.  
  21.  
  22.  
  23. @Override
  24.  
  25. public void onNext( Integer   integer ) {
  26.  
  27. Log.e(TAG, "onNext" );
  28.  
  29. }
  30.  
  31. });

After execution, only onCompleted will be output. The effect of this operator is just like the empty() method to create an empty Observable, and only the onCompleted() method will be executed. The difference is that ignoreElements processes the data source, while empty() creates an Observable.

<<:  RxJava Operator Series 3 (Part 1)

>>:  A collection of ViewHolder tool classes

Recommend

What key points should be paid attention to when operating WeChat mini programs?

Currently, the popularity of WeChat mini-programs...

How can online education retain traffic?

As an observer of the education and training indu...

How are the App Store rankings determined? How to get on the top of the rankings

In the past two years, Xu Huaizhe and Liu Xiong h...

Shenzhen Weather + Xinghua 400 website optimization SEO diagnostic analysis!

A few days ago, I found a website with less than ...

How to operate a good community?

Introduction: Although all operations have common...

How do girls react? What are girls' physiological reactions like?

How do girls react? When lovers and couples kiss,...

Wang Liuliu's 30-day traffic mining plan complete video

Want to know how to completely solve it within 30...