RxJava - Utility Operators


Advertisements

Following are the operators which are often useful with Observables.

Sr.No. Operator & Description
1

Delay

Register action to handle Observable life-cycle events.

2

Materialize/Dematerialize

Represents item emitted and notification sent.

3

ObserveOn

Specify the scheduler to be observed.

4

Serialize

Force Observable to make serialized calls.

5

Subscribe

Operate upon the emissions of items and notifications like complete from an Observable

6

SubscribeOn

Specify the scheduler to be used by an Observable when it is subscribed to.

7

TimeInterval

Convert an Observable to emit indications of the amount of time elapsed between emissions.

8

Timeout

Issues error notification if specified time occurs without emitting any item.

9

Timestamp

Attach timestamp to each item emitted.

9

Using

Creates a disposable resource or same lifespan as that of Observable.

Utility Operator Example

Create the following Java program using any editor of your choice in, say, C:\> RxJava.

ObservableTester.java

import io.reactivex.Observable;
//Using subscribe operator to subscribe to an Observable
public class ObservableTester  {
   public static void main(String[] args) {    
      String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
      final StringBuilder result = new StringBuilder();
      Observable<String> observable = Observable.fromArray(letters);
      observable.subscribe( letter -> result.append(letter));
      System.out.println(result);
   }
}

Verify the Result

Compile the class using javac compiler as follows −

C:\RxJava>javac ObservableTester.java

Now run the ObservableTester as follows −

C:\RxJava>java ObservableTester

It should produce the following output −

abcdefg
Advertisements