In this post, we will clarify Differences Between map() And flatMap() in Java 8 using an example
Below both Methods are available in java.util.stream.Stream Interface from Java 8 onwards
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
These two methods map and flatMap can be applied to a Stream<T> and they both return a Stream<R> only. The map(Function<? super T, ? extends R> mapper) method produces one output value for each input value but flatMap(Function<? super T, ? extends Stream<? extends R>> mapper) method produces an arbitrary number (zero or more) values for each input value.
Let’s try to understand the difference between these two methods using the below example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.infotech.client; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Differences Between map() And flatMap() in Java 8 *An example where we take a map of user names and lists of phones and "flatten" *it down to a list of phones of all the users using flatMap *@author KK JavaTutorials */ public class MapAndflatMapStreamOperationTest { public static void main(String[] args) { Map<String, List<String>> peopleNameAndPhonesMap = new HashMap<>(); peopleNameAndPhonesMap.put("KK", Arrays.asList("999-2256-766", "999-4467-788")); peopleNameAndPhonesMap.put("John", Arrays.asList("999-5523-877", "999-8766-877")); peopleNameAndPhonesMap.put("Sean", Arrays.asList("999-7766-654", "999-8766-754")); //Use of flatMap Stream to get all phones System.out.println("-------Use of flatMap Stream to get all phones--------"); List<String> phoneList = peopleNameAndPhonesMap.values().stream() .flatMap(Collection::stream).collect(Collectors.toList()); phoneList.forEach(System.out::println); //Use of map Stream to get all phones System.out.println("----------Use of map Stream to get all phones-----------"); List<Stream<String>> phonesList = peopleNameAndPhonesMap.values().stream() .map(Collection::stream).collect(Collectors.toList()); for (Stream<String> phoneStream : phonesList) { List<String> phones = phoneStream.collect(Collectors.toList()); System.out.println(phones); } System.out.println("----------Use of map stream to get all user names-------"); //Use of map stream to get all user names Set<String> namesList = peopleNameAndPhonesMap.keySet().stream().map(s->s).collect(Collectors.toSet()); namesList.forEach(System.out::println); } } |
Output of this Program:
——-Use of flatMap Stream to get all phones——–
999-2256-766
999-4467-788
999-5523-877
999-8766-877
999-7766-654
999-8766-754
———-Use of map Stream to get all phones———–
[999-2256-766, 999-4467-788]
[999-5523-877, 999-8766-877]
[999-7766-654, 999-8766-754]
———-Use of map stream to get all user names——-
KK
John
Sean
You May Also Like:
What are new Features added in Java 8?
When we choose LongAdder and LongAccumulator over AtomicLong & AtomicInteger ?
How can we maintain Immutability of a class with a mutable reference ?
That’s all about Differences Between map() And flatMap() in Java 8 ?
If you have any feedback or suggestion please feel free to drop in below comment box.