tutorial, flatmap, rx,

Rx - flatMap

Upendra Upendra Follow Jan 23, 2025 · 2 mins read
Rx - flatMap
Share this

flatMap() vs concatMap() vs switchMap()

FlatMap

Transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable.

The FlatMap operator transforms an Observable by applying a function that you specify to each item emitted by the source Observable, where that function returns an Observable that itself emits items. FlatMap then merges the emissions of these resulting Observables, emitting these merged results as its own sequence.

This method is useful, for example, when you have an Observable that emits a series of items that themselves have Observable members or are in other ways transformable into Observables, so that you can create a new Observable that emits the complete collection of items emitted by the sub-Observables of these items.

Note: FlatMap merges the emissions of these Observables and not care about the order of the items!

ConcatMap

Simillar to flatMap() but concatMap() maintains the order of items and waits for the current Observable to complete its job before emitting the next one.

Another difference between flatMap() and concatMap() operators. The flatMap() uses merge operator implicitly whereas concatMap() uses concat operator.

SwitchMap

Similar to flatMap(), except that it retains the result of only the latest observable, discarding the previous ones.

switchMap()would emit only the latest observable after a particular delay. It ignores all the previous ones.

http://reactivex.io/documentation/operators/flatmap.html
https://www.journaldev.com/19300/rxjava-flatmap-switchmap-concatmap
https://www.androidhive.info/RxJava/map-flatmap-switchmap-concatmap/
https://www.baeldung.com/rxjava-flatmap-switchmap
https://medium.com/appunite-edu-collection/rxjava-flatmap-switchmap-and-concatmap-differences-examples-6d1f3ff88ee0

credit goes to @swayangjit
Join Newsletter
Get the latest news right in your inbox. We never spam!
Upendra
Written by Upendra Follow
Hi, I am Upendra, the author in Human and machine languages,I don't know to how 3 liner bio works so just Connect with me on social sites you will get to know me better.