Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

RxJava provides various operators to transform items emitted by an observable into other observables. Two of the most popular operators are flatMap and switchMap. The difference between the two is often hard to understand for beginners in reactive programming.

For an introduction to RxJava, refer to this article.

In this tutorial, we'll understand the difference by walking through a simple example.

2. flatMap

The flatMap operator converts each item returned from a source observable into an independent observable using the supplied function and then merges all the observables into a single observable. The order in which observables are merged isn't guaranteed to be the same as in the source Observable.

Let's take a search engine as an example. Consider that we want to display search results immediately after we type in each character of the word:

For simplicity's sake, we have taken the search query input as a list of words.

Also, we always return two search results for each word.

// given
List<String> actualOutput = new ArrayList<>();
TestScheduler scheduler = new TestScheduler();
List<String> keywordToSearch = Arrays.asList("b", "bo", "boo", "book", "books");

// when
  .flatMap(s -> Observable.just(s + " FirstResult", s + " SecondResult")
    .delay(10, TimeUnit.SECONDS, scheduler))
  .doOnSuccess(s -> actualOutput.addAll(s))

scheduler.advanceTimeBy(1, TimeUnit.MINUTES);

// then
assertThat(actualOutput, hasItems("b FirstResult", "b SecondResult",
  "boo FirstResult", "boo SecondResult",
  "bo FirstResult", "bo SecondResult",
  "book FirstResult", "book SecondResult",
  "books FirstResult", "books SecondResult"));

Please note that the order isn't always the same with every run.

3. switchMap

The switchMap operator is similar to flatMap, except that it retains the result of only the latest observable, discarding the previous ones.

Let's change our requirement in that we want to get search results for only the final fully-formed word (in this case, “books”) and not for the partial query strings. To achieve this, we can use switchMap.

If we just replace flatMap with switchMap in the above code example, the following assertions would be valid:

assertEquals(2, actualOutput.size());
assertThat(actualOutput, hasItems("books FirstResult", "books SecondResult"));

As we see here, we only got a single observable containing the latest input item from the source observable. All previous results were discarded.

4. Conclusion

To summarize, switchMap differs from flatMap in that it only retains the output of applying a provided function to the latest item emitted by the source Observable, flatMap, on the other hand, retains all results and returns them in an interleaved manner without guaranteeing order.

As always, the code used in this article is available over on GitHub.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Comments are closed on this article!