flatMap()

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

The simplest application of flatMap() is to map one emission to many emissions. Let's say we want to emit the letters from each string coming from Observable<String>. We can use flatMap(Function<T,Observable<R>> mapper) to provide a function (implemented using a lambda expression) that maps each string to Observable<String>. Note that the mapped Observable<R> can emit any type R, different from the source T emissions. ...

Get Learning RxJava - Second Edition 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.