stream拓展

2022-07-23

java

开成花灾的玫瑰不是灿烂,而是荒凉。——严歌苓

源码地址:https://gitee.com/VampireAchao/stream-query/blob/master/stream-core/src/main/java/io/github/vampireachao/stream/core/stream/Steam.java

测试用例:

https://gitee.com/VampireAchao/stream-query/blob/master/stream-core/src/test/java/io/github/vampireachao/stream/core/stream/SteamTest.java

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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package io.github.vampireachao.stream.core.stream;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.Stream;

import static java.util.Collections.singletonList;

/**
* @author VampireAchao
* @since 2022/7/19 14:14
*/
class SteamTest {

@Test
void testBuilder() {
List<Integer> list = Steam.<Integer>builder().add(1).add(2).add(3).build().toList();
Assertions.assertEquals(Arrays.asList(1, 2, 3), list);
}

@Test
void testOf() {
Assertions.assertEquals(3, Steam.of(Arrays.asList(1, 2, 3), true).count());
Assertions.assertEquals(3, Steam.of(1, 2, 3).count());
Assertions.assertEquals(3, Steam.of(Stream.builder().add(1).add(2).add(3).build()).count());
}

@Test
void testSplit() {
List<Integer> list = Steam.split("1,2,3", ",").map(Integer::valueOf).toList();
Assertions.assertEquals(Arrays.asList(1, 2, 3), list);
}

@Test
void testIterator() {
List<Integer> list = Steam.iterate(0, i -> i < 3, i -> ++i).toList();
Assertions.assertEquals(Arrays.asList(0, 1, 2), list);
}

@Test
void testToCollection() {
List<Integer> list = Arrays.asList(1, 2, 3);
List<String> toCollection = Steam.of(list).map(String::valueOf).toColl(LinkedList::new);
Assertions.assertEquals(Arrays.asList("1", "2", "3"), toCollection);
}

@Test
void testToList() {
List<Integer> list = Arrays.asList(1, 2, 3);
List<String> toList = Steam.of(list).map(String::valueOf).toList();
Assertions.assertEquals(Arrays.asList("1", "2", "3"), toList);
}

@Test
void testToSet() {
List<Integer> list = Arrays.asList(1, 2, 3);
Set<String> toSet = Steam.of(list).map(String::valueOf).toSet();
Assertions.assertEquals(new HashSet<>(Arrays.asList("1", "2", "3")), toSet);
}

@Test
void testToZip() {
List<Integer> orders = Arrays.asList(1, 2, 3);
List<String> list = Arrays.asList("dromara", "hutool", "sweet");
Map<Integer, String> toZip = Steam.of(orders).toZip(list);
Assertions.assertEquals(new HashMap<Integer, String>() {{
put(1, "dromara");
put(2, "hutool");
put(3, "sweet");
}}, toZip);
}

@Test
void testJoin() {
List<Integer> list = Arrays.asList(1, 2, 3);
String joining = Steam.of(list).join();
Assertions.assertEquals("123", joining);
Assertions.assertEquals("1,2,3", Steam.of(list).join(","));
Assertions.assertEquals("(1,2,3)", Steam.of(list).join(",", "(", ")"));
}

@Test
void testToMap() {
List<Integer> list = Arrays.asList(1, 2, 3);
Map<String, Integer> identityMap = Steam.of(list).toMap(String::valueOf);
Assertions.assertEquals(new HashMap<String, Integer>() {{
put("1", 1);
put("2", 2);
put("3", 3);
}}, identityMap);
}

@Test
void testGroup() {
List<Integer> list = Arrays.asList(1, 2, 3);
Map<String, List<Integer>> group = Steam.of(list).group(String::valueOf);
Assertions.assertEquals(
new HashMap<String, List<Integer>>() {{
put("1", singletonList(1));
put("2", singletonList(2));
put("3", singletonList(3));
}}, group);
}

@Test
void testMapIdx() {
List<String> list = Arrays.asList("dromara", "hutool", "sweet");
List<String> mapIndex = Steam.of(list).mapIdx((e, i) -> i + 1 + "." + e).toList();
Assertions.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex);
// 并行流时为-1
Assertions.assertEquals(Arrays.asList(-1, -1, -1), Steam.of(1, 2, 3).parallel().mapIdx((e, i) -> i).toList());
}

@Test
void testMapMulti() {
List<Integer> list = Arrays.asList(1, 2, 3);
List<Integer> mapMulti = Steam.of(list).<Integer>mapMulti((e, buffer) -> {
if (e % 2 == 0) {
buffer.accept(e);
}
buffer.accept(e);
}).toList();
Assertions.assertEquals(Arrays.asList(1, 2, 2, 3), mapMulti);
}

@Test
void testDistinct() {
List<Integer> list = Arrays.asList(1, 2, 2, 3);
List<Integer> distinctBy = Steam.of(list).distinct(String::valueOf).toList();
Assertions.assertEquals(Arrays.asList(1, 2, 3), distinctBy);
}

@Test
void testForeachIdx() {
List<String> list = Arrays.asList("dromara", "hutool", "sweet");
Steam.Builder<String> builder = Steam.builder();
Steam.of(list).forEachIdx((e, i) -> builder.accept(i + 1 + "." + e));
Assertions.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), builder.build().toList());
// 并行流时为-1
Steam.of(1, 2, 3).parallel().forEachIdx((e, i) -> Assertions.assertEquals(-1, i));
}

@Test
void testForEachOrderedIdx() {
List<String> list = Arrays.asList("dromara", "hutool", "sweet");
Steam.Builder<String> builder = Steam.builder();
Steam.of(list).forEachOrderedIdx((e, i) -> builder.accept(i + 1 + "." + e));
Assertions.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), builder.build().toList());
}

@Test
void testFlatMapIdx() {
List<String> list = Arrays.asList("dromara", "hutool", "sweet");
List<String> mapIndex = Steam.of(list).flatMapIdx((e, i) -> Steam.of(i + 1 + "." + e)).toList();
Assertions.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex);
// 并行流时为-1
Assertions.assertEquals(Arrays.asList(-1, -1, -1), Steam.of(1, 2, 3).parallel().mapIdx((e, i) -> i).toList());
}

@Test
void testFlatMapIter() {
List<Integer> list = Arrays.asList(1, 2, 3);
List<Integer> flatMapIter = Steam.of(list).<Integer>flatMapIter(e -> null).toList();
Assertions.assertEquals(Collections.emptyList(), flatMapIter);
}

@Test
void testFilter() {
List<Integer> list = Arrays.asList(1, 2, 3);
List<Integer> filterIndex = Steam.of(list).filter(String::valueOf, "1").toList();
Assertions.assertEquals(Collections.singletonList(1), filterIndex);
}

@Test
void testFilterIdx() {
List<String> list = Arrays.asList("dromara", "hutool", "sweet");
List<String> filterIndex = Steam.of(list).filterIdx((e, i) -> i < 2).toList();
Assertions.assertEquals(Arrays.asList("dromara", "hutool"), filterIndex);
// 并行流时为-1
Assertions.assertEquals(3L, Steam.of(1, 2, 3).parallel().filterIdx((e, i) -> i == -1).count());
}

@Test
void testNonNull() {
List<Integer> list = Arrays.asList(1, null, 2, 3);
List<Integer> nonNull = Steam.of(list).nonNull().toList();
Assertions.assertEquals(Arrays.asList(1, 2, 3), nonNull);
}

@Test
void testParallel() {
Assertions.assertTrue(Steam.of(1, 2, 3).parallel(true).isParallel());
Assertions.assertFalse(Steam.of(1, 2, 3).parallel(false).isParallel());
}

@Test
void testPush() {
List<Integer> list = Arrays.asList(1, 2);
List<Integer> push = Steam.of(list).push(3).toList();
Assertions.assertEquals(Arrays.asList(1, 2, 3), push);
}

@Test
void testUnshift() {
List<Integer> list = Arrays.asList(2, 3);
List<Integer> unshift = Steam.of(list).unshift(1).toList();
Assertions.assertEquals(Arrays.asList(1, 2, 3), unshift);
}

@Test
void testAt() {
List<Integer> list = Arrays.asList(1, 2, 3);
Assertions.assertEquals(1, Steam.of(list).at(0));
Assertions.assertEquals(1, Steam.of(list).at(-3));
Assertions.assertEquals(3, Steam.of(list).at(-1));
Assertions.assertNull(Steam.of(list).at(-4));
}

@Test
void testSplice() {
List<Integer> list = Arrays.asList(1, 2, 3);
Assertions.assertEquals(Arrays.asList(1, 2, 2, 3), Steam.of(list).splice(1, 0, 2).toList());
Assertions.assertEquals(Arrays.asList(1, 2, 3, 3), Steam.of(list).splice(3, 1, 3).toList());
Assertions.assertEquals(Arrays.asList(1, 2, 4), Steam.of(list).splice(2, 1, 4).toList());
Assertions.assertEquals(Arrays.asList(1, 2), Steam.of(list).splice(2, 1).toList());
Assertions.assertEquals(Arrays.asList(1, 2, 3), Steam.of(list).splice(2, 0).toList());
Assertions.assertEquals(Arrays.asList(1, 2), Steam.of(list).splice(-1, 1).toList());
Assertions.assertEquals(Arrays.asList(1, 2, 3), Steam.of(list).splice(-2, 2, 2, 3).toList());
}

@Test
void testFindFirst() {
List<Integer> list = Arrays.asList(1, 2, 3);
Integer find = Steam.of(list).findFirst(Objects::nonNull);
Assertions.assertEquals(1, find);
}

@Test
void testFindFirstIdx() {
List<Integer> list = Arrays.asList(null, 2, 3);
Integer idx = Steam.of(list).findFirstIdx(Objects::nonNull);
Assertions.assertEquals(1, idx);
Assertions.assertEquals(-1, Steam.of(list).parallel().findFirstIdx(Objects::nonNull));
}

@Test
void testFindLast() {
List<Integer> list = Arrays.asList(1, null, 3);
Integer find = Steam.of(list).findLast(Objects::nonNull);
Assertions.assertEquals(3, find);
Assertions.assertEquals(3, Steam.of(list).findLast().orElse(null));
}

@Test
void testFindLastIdx() {
List<Integer> list = Arrays.asList(1, null, 3);
Integer idx = Steam.of(list).findLastIdx(Objects::nonNull);
Assertions.assertEquals(2, idx);
Assertions.assertEquals(-1, Steam.of(list).parallel().findLastIdx(Objects::nonNull));
}

@Test
void testZip() {
List<Integer> orders = Arrays.asList(1, 2, 3);
List<String> list = Arrays.asList("dromara", "hutool", "sweet");
List<String> zip = Steam.of(orders).zip(list, (e1, e2) -> e1 + "." + e2).toList();
Assertions.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
}
}