Site is under maintenance mode. Please wait few min!

flatMap in RxJava basic introduction for Beginners

Observable.flatMap() takes the emissions of one Observable and returns the emissions of another Observable to take its place.

Here’s how it works:

 

query("Hello, world!")
    .flatMap(new Func1, Observable>() {
        @Override
        public Observable call(List urls) {
            return Observable.from(urls);
        }
    })
    .subscribe(url -> System.out.println(url));



I’m showing the full function just so you can see exactly what happened, but simplified with lambdas it looks awesome:

query("Hello, world!")
    .flatMap(urls -> Observable.from(urls))
    .subscribe(url -> System.out.println(url));

Why is it returning another Observable? The key concept here is that the new Observable returned is what the Subscriber sees. It doesn’t receive a List – it gets a series of individual Strings as returned by Observable.from().

 

Categories: RxJava2

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Social Media Auto Publish Powered By : XYZScripts.com