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

Is it good or bad? The new Office 2016

Microsoft pointed out at the Windows 10 launch ev...

In the history of chemistry, there are 3 diamonds that make people laugh and cry

Diamond is the hardest substance known in nature....

Holistic thinking on operational product technology, you deserve it!

When a product is in its early stages, the produc...

Nine content marketing tips to help you create killer copywriting!

The prevalence of self-media has made us realize ...

37 public account editing skills that new media people must know!

When creating content for a public account, there...

Japan EV sales in 2024: BYD surpasses Toyota

On January 9, the Japan Automobile Importers Asso...

How much does it cost to make a car moving app in Zhangjiakou?

There is no fixed price for the production of Zha...

Cases + Tips | Create high-conversion information flow ads in 5 easy steps!

In the past two years, information flow advertisi...

Is WeChat Phonebook really causing operators to lose sleep?

On November 11, 2014, WeChat officially added a h...

APP promotion tips: 101 website promotion methods

People often ask me how to promote a website? Tod...

We will never forget these people in 2021!

2021 is about to pass This year Someone is coming...