Difference between Map and FlatMap in Java

The Stream API of Java 8 provides two intermediate stream operations used to perform actions on stream elements and return a stream as an output: map and flatMap. In this article, we are going to take a look at these two methods and explain the difference between them with examples.

Map function

The map function takes a Function as a parameter and applies it to all the elements of the stream. The result will be a stream of the result of the function applied to each element of the initial stream. There is a one-to-one mapping between the input and the output elements.

Let’s take a look at how we can use map:

List<String> digits = asList("one", "two", "three");
List<String> result = digits
	.stream()
	.map(String::toUpperCase)
	.collect(Collectors.toList());
assertEquals(asList("ONE", "TWO", "THREE"), result);

In this example, we have as input a list of strings in lowercase. The goal is to have the same list of words but in uppercase. To satisfy this requirement, map is the best choice: we will apply the function toUppercase of the String class to each element and collect the result in a list of Strings. The result will be [“ONE”, “TWO”, “THREE”].

FlatMap function

In case we have a more complex type like List<List<String>> as input, using the map method is not going to work for us since there is a nested list: We need to flatten first. Here where comes flatMap in action.

String sentence1 = "This is a";
String sentence2 = "flatMap example";
List<String> result = Stream.of(sentence1, sentence2) //Stream<String>
	.flatMap(sentence -> Stream.of(sentence.split(" "))) //Stream<String[]> => Stream<String>
	.map(String::toUpperCase)
	.collect(Collectors.toList());
assertEquals(asList("THIS", "IS", "A", "FLATMAP", "EXAMPLE"), result);

In this example, we want to have a list of all the words of sentence1 and sentence2 in uppercase. To do that, the flatMap operation will:

  • apply the split method on each sentence: produce an array of String. The result after this operation will be [“This”, “is”, “a”] and [“flatMap”, “example”].
  • flatten the resulting elements in a new stream: the output will be a stream of “This”, “is”, “a”,”flatMap”, “example”. Then we apply the toUpperCase operation on the elements of the stream. TheflatMap operation applies a one-to-many transformation to the elements of the stream and then flattens the nested collections into a new stream.

Summary

In this article, we presented two intermediate operations from the Stream API: mapand flatMap, and we explained how each operation works with examples.