forked from nrkapri/JavaAdvancedTopics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlatMapExample.java
More file actions
134 lines (90 loc) · 3.09 KB
/
Copy pathFlatMapExample.java
File metadata and controls
134 lines (90 loc) · 3.09 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package java8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class Student {
private String name;
private Set<String> book;
public void addBook(String book) {
if (this.book == null) {
this.book = new HashSet<>();
}
this.book.add(book);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<String> getBook() {
return book;
}
public void setBook(Set<String> book) {
this.book = book;
}
}
public class FlatMapExample {
public static void main(String[] args) {
/**
* In Java 8, Stream can hold different data types, for examples:
Stream<String[]>
Stream<Set<String>>
Stream<List<String>>
Stream<List<Object>>
Copy
But, the Stream operations (filter, sum, distinct…) and collectors do not support it, so, we need flatMap() to do the following conversion :
Stream<String[]> -> flatMap -> Stream<String>
Stream<Set<String>> -> flatMap -> Stream<String>
Stream<List<String>> -> flatMap -> Stream<String>
Stream<List<Object>> -> flatMap -> Stream<Object>
*/
String[][] data = new String[][]{{"a", "b"}, {"c", "d"}, {"e", "f"}};
//Stream<String[]>
Stream<String[]> temp = Arrays.stream(data);
//filter a stream of string[], and return a string[]?
Stream<String[]> stream = temp.filter(x -> "a".equals(x.toString()));
stream.forEach(System.out::println);
//returns empty
//use flatMap() to convert Stream<String[]> to Stream<String>.
String[][] data1 = new String[][]{{"a", "b"}, {"c", "d"}, {"e", "f"}};
//Stream<String[]>
Stream<String[]> temp1 = Arrays.stream(data1);
//Stream<String>, GOOD!
Stream<String> stringStream = temp1.flatMap(x -> Arrays.stream(x));
Stream<String> stream1 = stringStream.filter(x -> "a".equals(x.toString()));
stream1.forEach(System.out::println);
//Stream + Set + flatMap
Student obj1 = new Student();
obj1.setName("mkyong");
obj1.addBook("Java 8 in Action");
obj1.addBook("Spring Boot in Action");
obj1.addBook("Effective Java (2nd Edition)");
Student obj2 = new Student();
obj2.setName("zilap");
obj2.addBook("Learning Python, 5th Edition");
obj2.addBook("Effective Java (2nd Edition)");
List<Student> list = new ArrayList<>();
list.add(obj1);
list.add(obj2);
List<String> collect =
list.stream()
.map(x -> x.getBook()) //Stream<Set<String>>
.flatMap(x -> x.stream()) //Stream<String>
.distinct()
.collect(Collectors.toList());
collect.forEach(x -> System.out.println(x));
//Stream + Primitive + flatMapToInt
int[] intArray = {1, 2, 3, 4, 5, 6};
//1. Stream<int[]>
Stream<int[]> streamArray = Stream.of(intArray);
//2. Stream<int[]> -> flatMap -> IntStream
IntStream intStream = streamArray.flatMapToInt(x -> Arrays.stream(x));
intStream.forEach(x -> System.out.println(x));
}
}