Difference between map and flatMap

Category : Java | Sub Category : Java 8 Features | By Prasad Bonam Last updated: 2023-07-24 10:32:21 Viewed : 613


map and flatMap are higher-order functions commonly found in programming languages that support functional programming paradigms, such as Scala, Java, JavaScript, and others. These functions are used with collections, such as arrays or lists, to transform and manipulate their elements.

The key difference between map and flatMap lies in their behavior when applied to collections or streams of elements:

  1. map:
  • map is a higher-order function that applies a transformation function to each element in the collection or stream and returns a new collection or stream with the transformed elements.
  • The number of elements in the resulting collection or stream remains the same as the original collection or stream.
  • It maintains a one-to-one relationship between elements in the original collection and the resulting collection.
  • The transformation function can be used to convert each element to a different type, but it doesnt produce multiple elements for each input element.

Example using map:

java
List<Integer> numbers = Arrays.asList(1, 2, 3, 4); List<Integer> squares = numbers.stream() .map(num -> num * num) .collect(Collectors.toList()); // squares: [1, 4, 9, 16]

In this example, each number in the numbers list is squared using the map function, and the resulting stream contains the squares of the numbers.

  1. flatMap:
  • flatMap is also a higher-order function that applies a transformation function to each element in the collection or stream, but the difference is that the transformation function returns a collection or stream of elements for each input element.
  • The flatMap function then flattens all the collections or streams obtained from the transformation function into a single collection or stream, merging all the elements into one flat structure.
  • The number of elements in the resulting collection or stream may be different from the original collection or stream since each input element can produce multiple elements in the output.

Example using flatMap:

java
List<List<Integer>> listOfLists = Arrays.asList( Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9) ); List<Integer> flattenedList = listOfLists.stream() .flatMap(List::stream) .collect(Collectors.toList()); // flattenedList: [1, 2, 3, 4, 5, 6, 7, 8, 9]

In this example, flatMap is used to transform each nested list into a stream of elements, and then all those streams are flattened into a single list containing all the elements from the nested lists.

In summary, the primary distinction between map and flatMap is that map returns a collection or stream with the same number of elements, while flatMap returns a collection or stream that may have a different number of elements due to the flattening process. Additionally, map transforms elements one-to-one, while flatMap can transform elements to multiple elements and then flatten the result.


import java.util.Arrays;

import java.util.Collection;

import java.util.List;

import java.util.stream.Collectors;




Search
Related Articles

Leave a Comment: