RxJava Operator Map vs FlatMap
- Authors
- Name
- Amit Shekhar
- Published on
In RxJava, Map transforms each item emitted by an Observable by applying a function to it, whereas FlatMap transforms each item emitted by an Observable into another Observable.
In this blog, we will learn about the Map vs FlatMap Operator in RxJava.
I am Amit Shekhar, 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.
I teach AI and Machine Learning, and Android at Outcome School.
Join Outcome School and get a high-paying tech job:
Let's get started.
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.
FlatMap
FlatMap transforms the items emitted by an Observable into Observables.
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.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
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.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
Frequently Asked Questions
When should we use FlatMap instead of Map?
We should use FlatMap when we want to map over asynchronous operations. For example, after getting an ApiUser, we make a network call to get the UserDetail for that user. The FlatMap mapper returns an observable itself, which makes it the right fit for this case.
What does the Map mapper return compared to the FlatMap mapper?
The Map mapper returns a transformed item, while the FlatMap mapper returns an observable. For example, Map converts an ApiUser object into a User object. FlatMap, on the other hand, returns an ObservableSource such as the result of getUserDetailObservable(apiUser).
What is an operator in RxJava?
An operator is like a translator that translates or modifies data from one form to another form. The Observable is like a speaker and the Observer is like a listener. RxJava has so many operators, and we must know about them in order to use them correctly.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
