RxJava Operator - Map Vs FlatMap

Amit Shekhar
May 17, 2018 · 2 min read
Image for post
Image for post
RxJava Operator: Map vs FlatMap

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 the RxJava. It enables us to do complex things very simple. It provides us the power.

With great power comes great responsibility

Let’s see the components of the RxJava.

Image for post
Image for post
Components of the RxJava

The above diagram describes what each component does.

Operators: It translates the input into the required format of the output.

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.

Image for post
Image for post
Map Operator

FlatMap

FlatMap transforms the items emitted by an Observable into Observables.

Image for post
Image for post
FlatMap Operator

So, the main difference between Map and FlatMap 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

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

FlatMap Example

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.

Learn Complete RxJava in our professional course

That’s it for now. Happy learning :)

MindOrks

Our community publishes stories worth reading on Android…

Amit Shekhar

Written by

Working with the smartest people in the world to change the way we learn. EdTech products: MindOrks | AfterAcademy | CuriousJr | https://amitshekhar.me

MindOrks

MindOrks

Our community publishes stories worth reading on Android Development

Amit Shekhar

Written by

Working with the smartest people in the world to change the way we learn. EdTech products: MindOrks | AfterAcademy | CuriousJr | https://amitshekhar.me

MindOrks

MindOrks

Our community publishes stories worth reading on Android Development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store