rxjava transform operators

RxJava Transform Operators – Operators for Transforming Observables – RxJava Tutorial #5

RxJava Transform Operators, as the name indicates, are used for transforming the items which are emitted by reactive sources such as Observables.

In this article we will discuss about various RxJava Transform operators in depth with examples. Here we will discuss various operators like Buffer, Map, FlatMap, SwitchMap, ConcatMap, etc.

This article is part of RxJava Intro series. You can checkout the entire series here:

Now let us explore RxJava Transform Operators one by one.

RxJava Transform Operators
Buffer

This operator periodically gathers items emitted by an Observable into bundle and emits these bundles rather than emitting the items one at a time.

If the source Observable issues onError in between, then the Buffer immediately pass on this error notification on priority even if it contains some data that was emitted by the Observable before it issued error.

Example

In the above Example, in line 20, we can see that we used Observable.buffer() method to transform the way items are emitted from it.

Output

Map

This operator is used to transform items emitted by Observable by applying function to each item.

Example

In the above example, in line 21, we multiplied each item emitted by Observable with 10 using Map Operator thus transforming the data emitted by Observables.

Output:

Important Note: Notice that the order of insertion is same as that of the order of emission in case of Map Operator

FlatMap

FlatMap operator transforms the items emitted by Observable into Observables, by applying function to the items and then later, it flattens these items emitted by these Observables into a Single Observable. In other words, FlatMap merges Due to this flattening of the emissions, the order of emission is not maintained since the items emitted by these Observables are mixed (interleave).

Example:

Output:

In the above output, we can see that the order is not the same. Flatmap Operator does not preserve the order of items.

SwitchMap

The main difference between other operators and SwitchMap is the cancelling effect. SwitchMap operates in such a way that whenever a new item is emitted by Observable, it will unsubscribe to that old observable and begins mirroring the new one.

rxjava transform operators
Example

In the above example, in line 28, we can see the observable starts emitting the values in the time interval 750 milliseconds and again starts observing from 0 whenever the Observable from line 26 starts emitting new item.

Output

ConcatMap

ConcatMap operator works almost same as FlatMap, the only difference is –  ConcatMap preserves the order of emission of items. Let us take the same example which we used for FlatMap and use ConcatMap instead.

Example

 

Output

From the above output, we can see that the order of emission of items is preserved.

In the upcoming articles, we will discuss about RxJava filtering operators.

If you like what you’ve read today you can check our my other articles on Android and Kotlin development, or if want to get in touch, please send me a tweet or follow me on Twitter or on Facebook, it really makes my day.

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of