RxJava Operator Map vs FlatMap

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
RxJava Operator Map vs FlatMap

I am Amit Shekhar, a mentor helping developers in getting high-paying tech jobs.

In this blog, we will learn about the Map vs FlatMap Operator in RxJava.

RxJava is the most important library which is popular among Android developers. It makes our life easy.

We use RxJava for multithreading, managing background tasks, and removing callback hells. We can solve so many complex use cases with the help of RxJava. It enables us to do complex things very simply. It provides us the power.

With great power comes great responsibility

Let’s see the components of the RxJava.

Observable->Speaker
Operator->Translator
Observer->Listener

The above table describes what each component does.

Operator: An operator is like a translator who translates/modifies data from one form to another form.

RxJava has so many operators. In order to use them correctly, we must know about them. Here, we will discuss the Map and the FlatMap.

Map

Map transforms the items emitted by an Observable by applying a function to each item.

RxJava Map marble diagram

FlatMap

FlatMap transforms the items emitted by an Observable into Observables.

RxJava FlatMap marble diagram

So, the main difference between Map and FlatMap is that FlatMap mapper returns an observable itself, so it is used to map over asynchronous operations.

Very important: FlatMap is used to map over asynchronous operations.

Let’s see the example code.

Map Example:

getUserObservable()
    .map(new Function<ApiUser, User>() {
        @Override
        public User apply(ApiUser apiUser) throws Exception {
            // here we get the ApiUser from the server
            User user = new User(apiUser);
            // then by converting it into the user, we are returning
            return user;
        }
    })
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(getObserver());

Here, the observable gives us ApiUser object which we are converting into User object by using the map operator.

FlatMap Example:

getUserObservable()
    .flatMap(new Function<ApiUser, ObservableSource<UserDetail>>() {
        @Override
        public ObservableSource<UserDetail> apply(ApiUser apiUser) throws Exception {
            return getUserDetailObservable(apiUser);
        }
    })
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(getObserver());

Here, we are getting the ApiUser and then we are making a network call to get the UserDetail for that apiUser by using the getUserDetailObservable(apiUser). The flatMap mapper returns an observable itself. The getUserDetailObservable is an asynchronous operation.

This is how we should use the Map and the FlatMap operators in RxJava.

Master Kotlin Coroutines from here: Mastering Kotlin Coroutines

That's it for now.

Thanks

Amit Shekhar

You can connect with me on:

Read all of my high-quality blogs here.