In the tutorial, we will discover more aspect of Java 8 Stream API with flatMap()
function by lots of examples.
What we will do:
- Explain how Java 8 Stream FlatMap work?
- Apply Stream FlatMap on Java List, Array
Now let’s do more details!
Related posts:
– Java 8 Stream Map Examples
– Java 8 Stream Filter Examples
Java 8 Stream FlatMap
Java 8 Stream provides 2 mapping APIs: map() and flatMap()
– Both map()
& flatMap()
is an intermediate operation.
– Stream map()
function is used to transform an input stream to a new output stream by applying a mapper function on each element.
-> What is a difference point with Stream flatMap()
function?
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper); |
The flatMap()
function will do 2 things:
– Apply mapper function to transform each element of input stream.
– Then flattens the result to a new output stream. (Not with Java 8 Stream map()
function)
Stream FlatMap Java List Example
Stream FlatMap Integer List Example
We do a list of examples about Stream flatMap()
function combining with others: map()
, filter()
, reduce()
Example 1 – Flatten Map
Example:
List<List<Integer>> -> Stream -> FlatMap -> Stream -> Collectors.toList() -> List<Integer> |
– Code:
package com.grokonez.stream; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class StreamFlatMapExamples { public static void main(String[] args) { List<List<Integer>> listOfList = Arrays.asList( Arrays.asList(1, 2, 3, 4), Arrays.asList(5, 6, 7, 8), Arrays.asList(9, 10, 11, 12) ); System.out.println(listOfList); // [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] List<Integer> results = listOfList.stream() .flatMap(intList -> intList.stream()) .collect(Collectors.toList()); System.out.println(results); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] } } |
Example 2 – Combine flatMap() with map()
Example 2:
List<List<Integer>> -> Stream -> FlatMap (using Map) -> Stream -> Collectors.toList() -> List<Integer> |
Code:
List<List<Integer>> arraylist = Arrays.asList( Arrays.asList(1, 2, 3, 4), Arrays.asList(5, 6, 7, 8), Arrays.asList(9, 10, 11, 12) ); System.out.println(arraylist); // [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] List<Integer> results = arraylist.stream() .flatMap(intList -> intList.stream().map(i -> i*2)) .collect(Collectors.toList()); System.out.println(results); // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24] |
Example 3 – Combine flatMap() with reduce()
Example:
List<List<Integer>> -> Stream -> FlatMap -> Stream -> Reduce -> Int |
Code:
List<List<Integer>> listOfList = Arrays.asList( Arrays.asList(1, 2, 3, 4), Arrays.asList(5, 6, 7, 8), Arrays.asList(9, 10, 11, 12) ); System.out.println(listOfList); // [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] int sum = listOfList.stream() .flatMap(intList -> intList.stream()) .reduce(0, (i1, i2) -> i1 + i2); System.out.println(sum); // 78 |
Example 4 – Combine flatMap() with filter()
Example:
List<List<Integer>> -> Stream -> FlatMap -> Filter -> Stream -> List<Integer> |
Code:
List<List<Integer>> listOfList = Arrays.asList( Arrays.asList(1, 2, 3, 4), Arrays.asList(5, 6, 7, 8), Arrays.asList(9, 10, 11, 12) ); System.out.println(listOfList); // [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] List<Integer> results = listOfList.stream() .flatMap(intList -> intList.stream()) .filter( i-> i%2==0).collect(Collectors.toList()); System.out.println(results); // [2, 4, 6, 8, 10, 12] |
Stream FlatMap String List Example
Example:
List<List<String>> -> Stream -> flatMap() -> filter() -> Stream -> Collectors.toList() -> List<String> |
Code:
List<List<String>> listOfList = Arrays.asList( Arrays.asList("Java", "Django", "Vue", "Spring Cloud"), Arrays.asList("Python", "Spring Boot", "React"), Arrays.asList("Angular", "JQuery", "Spring JPA") ); System.out.println(listOfList); // [[Java, Django, Vue, Spring Cloud], [Python, Spring Boot, React], [Angular, JQuery, Spring JPA]] List<String> results = listOfList.stream() .flatMap(strList -> strList.stream()) .filter(str -> str.contains("Spring")).collect(Collectors.toList()); System.out.println(results); // [Spring Cloud, Spring Boot, Spring JPA] |
Stream FlatMap Custom Object List Example
– Create Developer class object:
class Developer { private String name; private Set<String> skills; public Developer(String name, Set<String> skills){ this.name = name; this.skills = skills; } public String getName() { return this.name; } public Set<String> getSkills(){ return this.skills; } public String toString() { return "{name = " + this.name + ", skills = " + this.skills + "}"; } } |
– Using Stream flatMap()
combine with distinct()
method to get all difference skill set of a List Developers.
Example:
List<Developer> -> Stream<Developer> -> FlatMap -> Stream<String> -> Distinct -> Stream<String> -> Collectors.toSet() -> Set<String> |
Code:
List<Developer> developers = Arrays.asList( new Developer("Jack", Stream.of("Java", "Node.js", "Angular") .collect(Collectors.toSet())), new Developer("Joe", Stream.of("C++", "ActiveMQ", "HTML", "CSS", "Java") .collect(Collectors.toSet())), new Developer("Peter", Stream.of("Python", "Node.js", "C++", "Vue.js") .collect(Collectors.toSet())), new Developer("Mary", Stream.of("Node.js", "Angular", "React", "CSS") .collect(Collectors.toSet())) ); System.out.println(developers); // [{name = Jack, skills = [Java, Node.js, Angular]}, {name = Joe, skills = [Java, C++, CSS, HTML, ActiveMQ]}, {name = Peter, skills = [Vue.js, C++, Node.js, Python]}, {name = Mary, skills = [CSS, Node.js, React, Angular]}] Set<String> skills = developers.stream() .flatMap(developer -> developer.getSkills().stream()) .distinct() .collect(Collectors.toSet()); System.out.println(skills); // [Java, Vue.js, C++, CSS, Node.js, HTML, ActiveMQ, React, Angular, Python] |
Stream FlatMap Java Array Example
To apply Stream FlatMap in Array with Java 8, We do 2 steps:
- Create Stream from Array Objects.
- Apply Stream FlatMap for Array Objects as the same way we had done with above List Objects.
– Modify Customer class:
class Developer { private String name; private String[] skills; public Developer(String name, String[] skills){ this.name = name; this.skills = skills; } public String getName() { return this.name; } public String[] getSkills(){ return this.skills; } public String toString() { return "{name = " + this.name + ", skills = " + this.skills + "}"; } } |
– Stream FlatMap Example with Java Array:
Developer[] developers = new Developer [] { new Developer("Jack", new String[] {"Java", "Node.js", "Angular"}), new Developer("Joe", new String[] {"C++", "ActiveMQ", "HTML", "CSS", "Java"}), new Developer("Peter", new String[] {"Python", "Node.js", "C++", "Vue.js"}), new Developer("Mary", new String[] {"Node.js", "Angular", "React", "CSS"}) }; System.out.println(developers); // [{name = Jack, skills = [Java, Node.js, Angular]}, {name = Joe, skills = [Java, C++, CSS, HTML, ActiveMQ]}, {name = Peter, skills = [Vue.js, C++, Node.js, Python]}, {name = Mary, skills = [CSS, Node.js, React, Angular]}] Set<String> skills = Arrays.stream(developers) .flatMap(developer -> Arrays.stream(developer.getSkills())) .distinct() .collect(Collectors.toSet()); System.out.println(skills); // [Java, Vue.js, C++, CSS, Node.js, HTML, ActiveMQ, React, Angular, Python] |
Conclusion
We had learned Java 8 Stream FlatMap with examples:
- Apply Java 8 Stream FlatMap to Integer, String and Custom Object List
- Combine Stream
flatMap()
function with others:map()
,reduce()
,filter()
,distinct()
- Apply Stream FlatMap to Java Array
Thanks for reading! See you later!