Java 8 Stream API map vs flatmap with example

Java 8 Stream API map vs flatMap with example

In this blog, we are going to see what is the difference between map and flatMap in the Java 8 stream API. If you want to learn more about the Java Stream API, please see the previous blog on Stream intermediate and terminal operations.

In most of the interviews, this was an important question asked in Java 8, and knowing their difference helps us to answer in the interview.

At first, both of them seem the same and they do the same thing but the underlying operation differs between them.

Let's see the map operation first.

map()

In stream API, map() is an intermediate operation that takes an input and performs the given operations on this input, and returns the output which is sent to the terminal operation.

Map() is used for transforming the given input into the desired output. It produces a single output for each given input value.

flatMap()

In stream API, flatMap() is an intermediate operation that takes an input and performs the given operations on this input and then flattens the result and sent to the terminal operation.

flatMap() is used for transforming + flattering the given input into the desired output. It produces multiple outputs for each given input value.

When to use map() and flatMap()

When you want to operate on a stream of input only transforming them into output then a map() operation is used.


Java Stream API flatmap vs map difference with example


When you have nested or complex data structures (stream inside stream) and want them into a single stream of output, the flatMap() operation is used.


Java Stream API flatmap vs map difference with example


Map and flatMap Examples

Let us see a simple example where we are going to multiply the number by 2.

Using map


List<Integer> numbers = Arrays.asList(1, 2, 3, 4);

List<Integer> output = numbers.stream().map(input -> input * 2).collect(Collectors.toList());

System.out.println(output);

In the above code, we are multiplying the input by 2 using the map function in a stream and storing the result in the list using Collectors.toList().

Using flatMap

Here we have a nested list that contains two numbers. The list is a bit complex as we have a list inside a list so in this kind of situation we can use flatMap.


List<Integer> number1 = Arrays.asList(1, 2);
List<Integer> number2 = Arrays.asList(3, 4);

List<List<Integer>> numberList = new ArrayList<>();
numberList.add(number1);
numberList.add(number2);

List<Integer> output = numberList.stream().flatMap(inputList -> inputList.stream().map(input -> input * 2))
	.collect(Collectors.toList());

System.out.println(output);

Using flatMap we are multiplying the list of input by 2 and returning a Stream of output (List of output) which is then flattened by flatMap and gives a single list of output.

Overview map vs flatMap

Map

  • The map is an intermediate operation in the stream API.
  • Takes a stream<T> as input and returns stream<R> as output
  • Used for transformation.
  • Produces a single value for each input value where it’s a one-to-one mapping.
  • Used when we have a simple input stream where we want the values to be transformed into another form.

FlatMap

  • The flatMap is an intermediate operation in the stream API.
  • Takes a stream<Stream<T» as input and returns stream<R> as output
  • Transforms and flatters (map() + flattering).
  • Produces multiple values for each input value where it’s a one-to-many mapping.
  • This is used when we have a complex (nested stream) input stream. The values are transformed into another form then the output is flattened into a single stream.

Post a Comment

Previous Post Next Post

Recent Posts

Facebook