RxJava - Utility Operators

RxJava - Utility Operators

An operator is a function that takes and alters the behavior of an upstream Observable<T> and returns a downstream Observable<R> or Subscriber

Below  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.

 

Example

public class RxUtility {
    public static void main(String args[])
    {
                    Observable<Character> values = Observable.using(
              () -> "RxJava",
                r -> {
                    return Observable.create(o -> {
                        for (Character c : r.toCharArray()) {
                            o.onNext(c);
                        }
                        o.onComplete();
                    });
                },
                r -> System.out.println("Disposed: " + r)
              );
            values.subscribe(new Consumer<Character>() {

                        @Override
                        public void accept(Character t) throws Exception {
                          System.out.println(t.toString());
                        }
                    });
              
            
    }
}

Output:

R
x
J
a
v
a
Disposed: RxJava