Repository navigation
Expand file tree
/
Copy pathFlatMap.java
More file actions
70 lines (49 loc) · 1.74 KB
/
Copy pathFlatMap.java
File metadata and controls
70 lines (49 loc) · 1.74 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
package dev.vpsop.intermediate;
import java.util.Arrays;
import java.util.List;
public class FlatMap {
public static void main(String[] args) {
List<List<Integer>> nums = List.of(
List.of(1, 2),
List.of(3, 4),
List.of(5, 6)
);
List<Integer> res1 = nums.stream()
.flatMap(x -> x.stream())
.toList();
System.out.println(res1);
List<Integer> res2 = nums.stream()
.flatMap(List::stream)
.toList();
System.out.println(res2);
List<String> sentences = List.of(
"Java is powerful",
"Streams are useful"
);
List<String> words = sentences.stream()
.flatMap(str -> Arrays.stream(str.split(" ")))
.toList();
System.out.println(words);
List<String> names = List.of("Alice", "Bob", "Charlie");
// List<char[]> characters = names.stream()
// .map(x -> x.toCharArray())
// .toList();
//
// System.out.println(characters);
List<Character> characters = names.stream()
.flatMapToInt(str -> str.chars())
.mapToObj(ch -> (char)ch)
.toList();
System.out.println(characters);
/// flatMapToInt(), flatMapToLong(), flatMapToDouble()
/// Same concept as flatMap(), but the result is a primitive stream
List<List<Integer>> nums2 = List.of(
List.of(1, 2),
List.of(3, 4)
);
int sum = nums2.stream()
.flatMapToInt(x -> x.stream().mapToInt(Integer::intValue))
.sum();
System.out.println(sum);
}
}