forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreams5.java
More file actions
138 lines (123 loc) · 4.04 KB
/
Streams5.java
File metadata and controls
138 lines (123 loc) · 4.04 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
135
136
137
138
package com.winterbe.java8.samples.stream;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* Testing the order of execution.
*
* @author Benjamin Winterberg
*/
public class Streams5 {
public static void main(String[] args) {
List<String> strings =
Arrays.asList("d2", "a2", "b1", "b3", "c");
// test1(strings);
// test2(strings);
// test3(strings);
// test4(strings);
// test5(strings);
// test6(strings);
// test7(strings);
test8(strings);
}
private static void test8(List<String> stringCollection) {
Supplier<Stream<String>> streamSupplier =
() -> stringCollection
.stream()
.filter(s -> s.startsWith("a"));
streamSupplier.get().anyMatch(s -> true);
streamSupplier.get().noneMatch(s -> true);
}
// stream has already been operated upon or closed
private static void test7(List<String> stringCollection) {
Stream<String> stream = stringCollection
.stream()
.filter(s -> s.startsWith("a"));
stream.anyMatch(s -> true);
stream.noneMatch(s -> true);
}
// short-circuit
private static void test6(List<String> stringCollection) {
stringCollection
.stream()
.map(s -> {
System.out.println("map: " + s);
return s.toUpperCase();
})
.anyMatch(s -> {
System.out.println("anyMatch: " + s);
return s.startsWith("A");
});
}
private static void test5(List<String> stringCollection) {
stringCollection
.stream()
.filter(s -> {
System.out.println("filter: " + s);
return s.toLowerCase().startsWith("a");
})
.sorted((s1, s2) -> {
System.out.printf("sort: %s; %s\n", s1, s2);
return s1.compareTo(s2);
})
.map(s -> {
System.out.println("map: " + s);
return s.toUpperCase();
})
.forEach(s -> System.out.println("forEach: " + s));
}
// sorted = horizontal
private static void test4(List<String> stringCollection) {
stringCollection
.stream()
.sorted((s1, s2) -> {
System.out.printf("sort: %s; %s\n", s1, s2);
return s1.compareTo(s2);
})
.filter(s -> {
System.out.println("filter: " + s);
return s.toLowerCase().startsWith("a");
})
.map(s -> {
System.out.println("map: " + s);
return s.toUpperCase();
})
.forEach(s -> System.out.println("forEach: " + s));
}
private static void test3(List<String> stringCollection) {
stringCollection
.stream()
.filter(s -> {
System.out.println("filter: " + s);
return s.startsWith("a");
})
.map(s -> {
System.out.println("map: " + s);
return s.toUpperCase();
})
.forEach(s -> System.out.println("forEach: " + s));
}
private static void test2(List<String> stringCollection) {
stringCollection
.stream()
.map(s -> {
System.out.println("map: " + s);
return s.toUpperCase();
})
.filter(s -> {
System.out.println("filter: " + s);
return s.startsWith("A");
})
.forEach(s -> System.out.println("forEach: " + s));
}
private static void test1(List<String> stringCollection) {
stringCollection
.stream()
.filter(s -> {
System.out.println("filter: " + s);
return true;
})
.forEach(s -> System.out.println("forEach: " + s));
}
}