flatMap()

One of the most powerful and critical operators in RxJava is flatMap(). If you have to invest time in understanding any RxJava operator, this is the one. It is an operator that performs a dynamic Observable.merge() by taking each emission and mapping it to an Observable. Then, it merges the emissions from the resulting Observables into a single stream.

The simplest application of flatMap() is to map one emission to many emissions. Say, we want to emit the characters from each string coming from Observable<String>. We can use flatMap() to specify a Function<T,Observable<R>> lambda that maps each string to an Observable<String>which will emit the letters. Note that the mapped Observable<R> can emit any type R, different from the ...

Get Learning RxJava now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.