O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

FlatMap Observables

.flatMap() is a map operation--it transforms one value to another. However, the way it does it is a bit different. Let's examine this with an example where we have a bunch of IDs, and we need to request some remote service to receive data:

Observable.just("ID1", "ID2", "ID3")        .map(id -> Observable.fromCallable(mockHttpRequest(id)))        .subscribe(e -> log(e.toString()));

Here, we have three IDs and each of them will initiate a new request using our mockHttpRequest method; that implementation detail is not important here, let's assume that it returns a working Callable interface instance. For the sake of argument, we can set it to something like this:

private Callable<Date> mockHttpRequest(String id) {    return Date::new

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required