RxJava Operator Map vs FlatMap

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

I am Amit Shekhar, Co-Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

Join Outcome School and get high paying tech job: Outcome School

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

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.

Prepare yourself for Android Interview: Android Interview Questions

That's it for now.

Thanks

Amit Shekhar
Co-Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.