-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapVsFlatMapMain.java
More file actions
40 lines (32 loc) · 1.07 KB
/
Copy pathMapVsFlatMapMain.java
File metadata and controls
40 lines (32 loc) · 1.07 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
package stream.operation;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class MapVsFlatMapMain {
public static void main(String[] args) {
List<List<Integer>> outerList = List.of(
List.of(1,2),
List.of(3,4),
List.of(5,6)
);
System.out.println("outerList = " + outerList);
// for
List<Integer> forResult = new ArrayList<>();
for (List<Integer> list : outerList) {
for (Integer result : list) {
forResult.add(result);
}
}
System.out.println("forResult = " + forResult);
// map
List<Stream<Integer>> stream = outerList.stream()
.map(list -> list.stream())
.toList();
System.out.println("stream = " + stream);
// flatMap
List<Integer> flatMapResult = outerList.stream()
.flatMap(list -> list.stream())
.toList();
System.out.println("flatMapResult = " + flatMapResult);
}
}