-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlatMapExamples.java
More file actions
49 lines (36 loc) · 1.32 KB
/
Copy pathFlatMapExamples.java
File metadata and controls
49 lines (36 loc) · 1.32 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
package streamInterviewQA;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/***
* It’s challenging to process a Stream containing more than one level, like
* Stream<String[]> or Stream<List<LineItem>> or Stream<Stream<String>>. And we
* flat the 2 levels Stream into one level, like Stream<String> or
* Stream<LineItem>, so that we can easily loop the Stream and process it.
**/
public class FlatMapExamples {
static Predicate<String[]> filterNotA = (p) -> {
for (String s : p) {
if (s.equals("a"))
return false;
}
return true;
};
public static void flatMapExample(String[][] array) {
List<String> collect = Stream.of(array) // Stream<String[]>
.flatMap(Stream::of) // Stream<String>
.filter(x -> !"a".equals(x)) // filter out the a
.collect(Collectors.toList()); // return a List
collect.forEach(System.out::println);
}
public static void main(String[] args) {
String[][] array = new String[][] { { "a", "b" }, { "c", "d" }, { "e", "f" } };
// array to a stream
Stream<String[]> stream1 = Arrays.stream(array);
List<String[]> result = stream1.filter(filterNotA).collect(Collectors.toList());
result.forEach(x -> System.out.println(Arrays.toString(x)));
flatMapExample(array);
}
}