Repository navigation
Expand file tree
/
Copy pathChapter08.java
More file actions
232 lines (188 loc) · 6.47 KB
/
Copy pathChapter08.java
File metadata and controls
232 lines (188 loc) · 6.47 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import ch08.ex04.Random;
import ch08.ex05.FilterMap;
import ch08.ex06.StringStream;
import ch08.ex11.StreamUtils;
import ch08.ex12.Merger;
import org.apache.commons.lang3.time.StopWatch;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class Chapter08 {
private List<String> alice;
private List<String> warandpeace;
@Before
public void before() throws IOException {
String contents1 = new String(Files.readAllBytes(Paths.get("docs/ch08/alice.txt")));
alice = Arrays.asList(contents1.split("\\PL+"));
String contents2 = new String(Files.readAllBytes(Paths.get("docs/ch08/warandpeace.txt")));
warandpeace = Arrays.asList(contents2.split("\\PL+"));
}
@Test
public void ex01() {
List<String> result = warandpeace.stream().peek(s -> System.out.println("peak = " + s)).filter(s -> s.length() > 12).limit(5).collect(Collectors.toList());
System.out.println("result = " + result);
}
@Test
public void ex02() {
// using commons-lang3
StopWatch stopwatch = new StopWatch();
stopwatch.reset();
stopwatch.start();
long c1 = warandpeace.stream().filter(s -> s.length() > 12).count();
stopwatch.stop();
System.out.println("stream = " + stopwatch.toString());
stopwatch.reset();
stopwatch.start();
long c2 = warandpeace.parallelStream().filter(s -> s.length() > 12).count();
stopwatch.stop();
System.out.println("parallelStream = " + stopwatch.toString());
}
@Test
public void ex03() {
int[] values = {1, 4, 9, 16};
IntStream stream = IntStream.of(values);
}
@Test
public void ex04() {
Stream<Long> stream = Random.get(10, 2521490397L, 11, (long) Math.pow(2, 48)).limit(10);
Iterator<Long> iterator = stream.iterator();
while (iterator.hasNext()) {
Long l = iterator.next();
System.out.println("l = " + l);
}
}
@Test
public void ex05() {
Stream<String> letters = FilterMap.letters("boat");
System.out.println(Arrays.toString(letters.toArray()));
}
@Test
public void ex06() {
boolean bool = StringStream.isWord("antop");
System.out.println("bool = " + bool);
}
@Test
public void ex07() throws IOException {
// filter word
Stream<String> filtered = warandpeace.stream().filter(s -> StringStream.isWord(s));
// to lower case
Stream<String> toLowerCase = filtered.map(String::toLowerCase);
// grouping (word :: counting)
Map<String, Long> grouping = toLowerCase.collect(Collectors.groupingBy(String::new, Collectors.counting()));
// map -> stream -> sort
Stream<Map.Entry<String, Long>> sorted = grouping.entrySet().stream().sorted(Map.Entry.<String, Long>comparingByValue().reversed()).limit(10);
// print
sorted.forEach(System.out::println);
}
@Test
public void ex08() {
}
@Test
public void ex09() {
OptionalDouble average = warandpeace.stream().mapToInt(value -> value.length()).average();
average.ifPresent(value -> System.out.println("average = " + (int) value));
}
@Test
public void ex10() {
// grouping (length :: list)
Map<Integer, List<String>> grouping = alice.stream().collect(Collectors.groupingBy(String::length));
// get max length
Optional<Map.Entry<Integer, List<String>>> max = grouping.entrySet().stream().collect(Collectors.maxBy(Map.Entry.comparingByKey()));
// print
max.ifPresent(e -> System.out.println(e.getValue()));
}
@Test
public void ex11() {
Stream<Double> stream = Stream.generate(() -> Math.random());
boolean bool = StreamUtils.isFinite(stream);
System.out.println("isFinite = " + bool);
}
@Test
public void ex12() {
Stream<String> s1 = alice.stream().skip(100).limit(12);
Stream<String> s2 = warandpeace.stream().skip(100).limit(10);
Stream<String> zip = Merger.zip(s1, s2);
//zip.forEachOrdered(System.out::println);
}
@Test
public void ext13() {
ArrayList<String> list1 = new ArrayList<>(Arrays.asList("a", "b", "c"));
ArrayList<String> list2 = new ArrayList<>(Arrays.asList("d", "e", "f"));
ArrayList<String> list3 = new ArrayList<>(Arrays.asList("g", "h", "i"));
ArrayList<String> list4 = new ArrayList<>(Arrays.asList("j", "k", "l"));
ArrayList<String> list5 = new ArrayList<>(Arrays.asList("m", "n", "o"));
Stream<ArrayList<String>> stream = Stream.of(list1, list2, list3, list4, list5);
/*
Optional<ArrayList<String>> reduce = stream.reduce((x, y) -> {
System.out.println("x = " + x + ", y = " + y);
ArrayList<String> list = new ArrayList<>();
list.addAll(x);
list.addAll(y);
return list;
});
reduce.ifPresent(v -> System.out.println("result = " + v));
System.out.println("list1 = " + list1);
*/
/*
ArrayList<String> reduce = stream.reduce(new ArrayList<String>(), (x, y) -> {
x.addAll(y);
return x;
});
System.out.println("result = " + reduce);
*/
ArrayList<String> reduce = stream.parallel().reduce(new ArrayList<String>(), (x, y) -> {
System.out.println("accumulator = " + Thread.currentThread().getName() + ", x = " + x + ", y = " + y);
ArrayList<String> accumulator = new ArrayList<>();
accumulator.addAll(x);
accumulator.addAll(y);
return accumulator;
}, (x, y) -> {
System.out.println("combiner = " + Thread.currentThread().getName() + ", x = " + x + ", y = " + y);
ArrayList<String> combiner = new ArrayList<>();
combiner.addAll(x);
combiner.addAll(y);
return combiner;
});
System.out.println("result = " + reduce);
}
@Test
public void ex14() {
Stream<Double> stream = Stream.generate(() -> Math.random()).limit(20);
Optional<Double> reduce = stream.reduce(Double::sum);
reduce.ifPresent(d -> System.out.println(d / stream.count()));
}
@Test
public void ex15() {
}
@Test
public void ex16() {
StopWatch stopwatch = new StopWatch();
stopwatch.reset();
stopwatch.start();
warandpeace.stream().sorted(Comparator.comparing(String::length).reversed()).limit(500).count();
stopwatch.stop();
System.out.println("stream = " + stopwatch.toString());
stopwatch.reset();
stopwatch.start();
warandpeace.parallelStream().sorted(Comparator.comparing(String::length).reversed()).limit(500).count();
stopwatch.stop();
System.out.println("parallelStream = " + stopwatch.toString());
}
@Test
public void test() {
System.out.println("Length is "
+ Stream.generate(Math::random)
.spliterator()
.estimateSize());
System.out.println(Long.MAX_VALUE);
}
}