Map .

Difference Between Map And Flatmap In Java 8

Written by Ben Javu Dec 14, 2022 ยท 4 min read
Difference Between Map And Flatmap In Java 8

<code>List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); List<Integer> doubledNumbers = numbers.stream() .map(num -> num * 2) .collect(Collectors.toList());</code>

Table of Contents

Difference between map() and flatMap() in Java 8 Stream
Difference between map() and flatMap() in Java 8 Stream from javarevisited.blogspot.com

Introduction

If you are a Java developer, then you must be familiar with the terms map and flatMap. These are two important functions in Java 8 that help to process and manipulate data in a stream. However, understanding the differences between them can be a bit tricky. In this article, we will explore the differences between map and flatMap in Java 8.

Map Function in Java 8

The map() function is used to transform the elements of a stream into another form. It takes a Function as an argument, where T is the type of the input elements, and R is the type of the output elements. The map() function applies this function to each element of the stream and returns a new stream consisting of the transformed elements. For example, let's say we have a stream of integers and we want to double each element. We can use the map() function as follows:

List numbers = Arrays.asList(1, 2, 3, 4, 5); List doubledNumbers = numbers.stream() .map(num -> num * 2) .collect(Collectors.toList());

In this example, we first create a list of integers and then use the stream() function to create a stream from it. We then use the map() function to double each element of the stream, and finally, we collect the transformed elements into a new list.

FlatMap Function in Java 8

The flatMap() function is used to transform a stream of streams into a single stream. It takes a Function> as an argument, where T is the type of the input elements, and Stream is the type of the output elements. The flatMap() function applies this function to each element of the stream and then flattens the resulting streams into a single stream. For example, let's say we have a list of lists of integers and we want to flatten it into a single list. We can use the flatMap() function as follows:

List> listOfLists = Arrays.asList( Arrays.asList(1, 2), Arrays.asList(3, 4), Arrays.asList(5, 6) ); List flattenedList = listOfLists.stream() .flatMap(list -> list.stream()) .collect(Collectors.toList());

In this example, we first create a list of lists of integers. We then use the stream() function to create a stream from it. We then use the flatMap() function to flatten the list of lists into a single list, and finally, we collect the flattened list into a new list.

Differences Between Map and FlatMap

The main difference between map() and flatMap() is that map() transforms each element of a stream into another element, whereas flatMap() transforms each element of a stream into zero or more elements. In other words, map() produces a one-to-one mapping between the elements of the input stream and the elements of the output stream, whereas flatMap() produces a one-to-many mapping. Another difference is that map() returns a stream of the same type as the input stream, whereas flatMap() returns a stream of a potentially different type. This is because the flatMap() function can transform each element of the input stream into a stream of a different type.

Question & Answer

Q. Can we use map() and flatMap() together?

Yes, we can use map() and flatMap() together. In fact, it is quite common to do so. For example, let's say we have a list of strings representing sentences, and we want to find all the distinct words in these sentences. We can use the map() function to split each sentence into words, and then use the flatMap() function to flatten the resulting stream of streams into a single stream of words.

Q. When should we use map() and when should we use flatMap()?

We should use map() when we want to transform each element of a stream into another element. We should use flatMap() when we want to transform each element of a stream into zero or more elements. In general, if the transformation function returns a single element, we should use map(). If the transformation function returns a collection or a stream of elements, we should use flatMap().
Read next