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:
- RxAndroid Introduction
- RxAndroid Example – RxJava Tutorial #2
- RxJava Operators In General – RxJava Tutorial #3
- RxJava Operators for Creating Observables – RxJava Tutorial #4
- RxJava Operators for Transforming Observables – RxJava Tutorial #5 – You are here
- RxJava Operators for Filtering Observables – RxJava Tutorial #6 – Coming Up
- RxJava Operators for Combining Observables – RxJava Tutorial #7 – Coming Up
Now let us explore RxJava Transform Operators one by one.
RxJava Transform Operators
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.
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.
This operator is used to transform items emitted by Observable by applying function to each item.
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.
Important Note: Notice that the order of insertion is same as that of the order of emission in case of Map Operator
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).
In the above output, we can see that the order is not the same. Flatmap Operator does not preserve the order of items.
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.
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.
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.
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.