Common operators used in RxJava

Tram Ho

As learn in part 1 about Rx in this article, I will focus on the operators used in Rx java

— Creating Observables

1.just()

As the name of the function, the operator emits only the same values provided in the arguments.


VD : Observable.just(1, 2, 3, 4, 5)

Here, our observable with just operator has been applied. So the observable will emit between 1 and 5 integers.

  • There is another operator: from() which takes an array of an object as input and emits the object in turn just like the just() operator. Below is the code to generate integer from 1 to 5 using from() operator. Observable.from(new Integer[]{1, 2, 3, 4, 5});

— Filtering Operators:

  • Filter operators filter out stream objects based on certain expressions and emit only data objects that satisfy the expression.
    1.filter() :
    Sometimes we want to tweak specific events that are only emitted by the observable. Let’s say we want to emit only odd numbers out of the elements emitted by the Observable. We can achieve this using an operator called filter().

As the name suggests the filter operator, filters the items emitted by an Observable. Using filter() we can decide whether to emit the object or not based on some condition.

image.png

2. skip():
skip(n) will discard the first n items emitted by an Observable and emit data after n elements. So skip(2) will skip playing the first 2 elements and start from emitting the 3rd element.

So for our example, if we apply skip(2) operator, it will emit only 3, 4 and 5 integers.

image.png

3. take(): take() is the opposite of the skip() operator. take(n) will emit only the first n data elements and skip all data elements after the n emitted elements.

  • takeLast() will emit only the last element from the data stream.
  • Contrary to takeLast() the takeFirst() operator will emit only the first element of the data stream and skip the subsequent data elements.

— Combining Operators:

  • Operators work with multiple observables to create a single observable

1. merge():
The merge() operator combines multiple Observables into one by merging the elements emitted by the Observables. The merge() operator can interleave outputs because it emits data from both Observables. simultaneously as soon as data is available to play.

2. zip():
zip() combines the emission of multiple operators together through a specified function and emits single entries for each combination based on the result of this function.

Here is an example where you can combine strings and integer data streams into a ZipObject (custom class) and emit them as the final data stream.

image.png

— Mathematical and Aggregate Operators

1. concat(): The Concat operator concatenates the output of multiple Observables so that they behave like a single Observable.

image.png

Let’s say you have two observables. Observable1 emits integers from 1 to 5 respectively and an Observable2 emits integers from 6 to 10. If we combine them, Observable1 will emit from 1 to 5 and Observable2 will emit data from 6 to 10 at the same time. The concat() operator will merge both the data stream and emit data from Observable1 and then emit data from Observable2. Below is an explanatory diagram.

References:

Share the news now