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

Yan Jie 14-day extreme waist and abdomen shaping

Yan Jie's 14-day ultimate waist and abdomen s...

The little secret of the park: making the city "alive"

Biodiversity Conservation From the Wild to the Ci...

Git Notes

[[144417]] Preface Git is indeed a very good vers...

What is it like when WeChat and Alipay meet the highway?

From the development of Internet payment to the c...

Google's upcoming Android P features leaked

It is now March, and the official release of Andr...

App numbers may turn WeChat into an “internet operating system”

[[162381]] Recently, many entrepreneurs who want ...

An important discovery: TRAPPIST-1 habitable planet orbit is not misplaced!

[Mobile software: Bo Ke Yuan] Astronomers using t...

How to become a Douyin Blue V certified promoter? How much do I need to pay?

What is the Douyin Blue V certification project? ...

Event operation execution and design!

When asked: What is the purpose of holding activi...

It’s scary! Windows 10 can collect so much personal privacy

There are many controversies about Windows 10, one...