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. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access