Skip to content

Commit 61cc975

Browse files
committed
feat: add collectors
1 parent db91ebf commit 61cc975

4 files changed

Lines changed: 125 additions & 15 deletions

File tree

src/main/java/com/github/lokic/javaplus/Collectors.java renamed to src/main/java/com/github/lokic/javaplus/OtherCollectors.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
import com.github.lokic.javaplus.tuple.Tuple2;
66

77
import java.util.*;
8-
import java.util.function.BinaryOperator;
9-
import java.util.function.Function;
10-
import java.util.function.Supplier;
8+
import java.util.function.*;
119
import java.util.stream.Collector;
1210
import java.util.stream.Stream;
1311

14-
public class Collectors {
12+
public class OtherCollectors {
1513

1614
public static class Reversed {
1715

@@ -193,4 +191,36 @@ private enum Order {
193191
java.util.stream.Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapSupplier),
194192
m -> m.entrySet().stream().map(e -> Tuple.of(e.getKey(), e.getValue())));
195193
}
194+
195+
196+
public static <T, A, R>
197+
Collector<T, ?, R> filtering(Predicate<? super T> predicate,
198+
Collector<? super T, A, R> downstream) {
199+
BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator();
200+
return Collector.of(downstream.supplier(),
201+
(r, t) -> {
202+
if (predicate.test(t)) {
203+
downstreamAccumulator.accept(r, t);
204+
}
205+
},
206+
downstream.combiner(), downstream.finisher(),
207+
downstream.characteristics().toArray(new Collector.Characteristics[0]));
208+
}
209+
210+
public static <T, U, A, R>
211+
Collector<T, ?, R> flatMapping(Function<? super T, ? extends Stream<? extends U>> mapper,
212+
Collector<? super U, A, R> downstream) {
213+
BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator();
214+
return Collector.of(downstream.supplier(),
215+
(r, t) -> {
216+
try (Stream<? extends U> result = mapper.apply(t)) {
217+
if (result != null)
218+
result.sequential().forEach(u -> downstreamAccumulator.accept(r, u));
219+
}
220+
},
221+
downstream.combiner(), downstream.finisher(),
222+
downstream.characteristics().toArray(new Collector.Characteristics[0]));
223+
}
224+
225+
196226
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.github.lokic.javaplus;
2+
3+
import com.github.lokic.javaplus.tuple.*;
4+
5+
import java.util.LinkedHashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Objects;
9+
import java.util.stream.Collector;
10+
11+
import static com.github.lokic.javaplus.OtherCollectors.filtering;
12+
13+
public class TupleCollectors {
14+
15+
public static <T1, T2>
16+
Collector<Tuple2<T1, T2>, ?, Map<T1, List<T2>>> groupingBy1() {
17+
return java.util.stream.Collectors.groupingBy(
18+
Tuple2::getT1,
19+
LinkedHashMap::new,
20+
java.util.stream.Collectors.mapping(Tuple2::getT2, filtering(Objects::nonNull, java.util.stream.Collectors.toList())));
21+
}
22+
23+
public static <T1, T2, T3>
24+
Collector<Tuple3<T1, T2, T3>, ?, Map<Tuple2<T1, T2>, List<T3>>> groupingBy2() {
25+
return java.util.stream.Collectors.groupingBy(
26+
t -> Tuple.of(t.getT1(), t.getT2()),
27+
LinkedHashMap::new,
28+
java.util.stream.Collectors.mapping(Tuple3::getT3, filtering(Objects::nonNull, java.util.stream.Collectors.toList())));
29+
}
30+
31+
public static <T1, T2, T3, T4>
32+
Collector<Tuple4<T1, T2, T3, T4>, ?, Map<Tuple3<T1, T2, T3>, List<T4>>> groupingBy3() {
33+
return java.util.stream.Collectors.groupingBy(
34+
t -> Tuple.of(t.getT1(), t.getT2(), t.getT3()),
35+
LinkedHashMap::new,
36+
java.util.stream.Collectors.mapping(Tuple4::getT4, filtering(Objects::nonNull, java.util.stream.Collectors.toList())));
37+
}
38+
39+
public static <T1, T2, T3, T4, T5>
40+
Collector<Tuple5<T1, T2, T3, T4, T5>, ?, Map<Tuple4<T1, T2, T3, T4>, List<T5>>> groupingBy4() {
41+
return java.util.stream.Collectors.groupingBy(
42+
t -> Tuple.of(t.getT1(), t.getT2(), t.getT3(), t.getT4()),
43+
LinkedHashMap::new,
44+
java.util.stream.Collectors.mapping(Tuple5::getT5, filtering(Objects::nonNull, java.util.stream.Collectors.toList())));
45+
}
46+
47+
public static <T1, T2, T3, T4, T5, T6>
48+
Collector<Tuple6<T1, T2, T3, T4, T5, T6>, ?, Map<Tuple5<T1, T2, T3, T4, T5>, List<T6>>> groupingBy5() {
49+
return java.util.stream.Collectors.groupingBy(
50+
t -> Tuple.of(t.getT1(), t.getT2(), t.getT3(), t.getT4(), t.getT5()),
51+
LinkedHashMap::new,
52+
java.util.stream.Collectors.mapping(Tuple6::getT6, filtering(Objects::nonNull, java.util.stream.Collectors.toList())));
53+
}
54+
55+
public static <T1, T2, T3, T4, T5, T6, T7>
56+
Collector<Tuple7<T1, T2, T3, T4, T5, T6, T7>, ?, Map<Tuple6<T1, T2, T3, T4, T5, T6>, List<T7>>> groupingBy6() {
57+
return java.util.stream.Collectors.groupingBy(
58+
t -> Tuple.of(t.getT1(), t.getT2(), t.getT3(), t.getT4(), t.getT5(), t.getT6()),
59+
LinkedHashMap::new,
60+
java.util.stream.Collectors.mapping(Tuple7::getT7, filtering(Objects::nonNull, java.util.stream.Collectors.toList())));
61+
}
62+
63+
public static <T1, T2, T3, T4, T5, T6, T7, T8>
64+
Collector<Tuple8<T1, T2, T3, T4, T5, T6, T7, T8>, ?, Map<Tuple7<T1, T2, T3, T4, T5, T6, T7>, List<T8>>> groupingBy7() {
65+
return java.util.stream.Collectors.groupingBy(
66+
t -> Tuple.of(t.getT1(), t.getT2(), t.getT3(), t.getT4(), t.getT5(), t.getT6(), t.getT7()),
67+
LinkedHashMap::new,
68+
java.util.stream.Collectors.mapping(Tuple8::getT8, filtering(Objects::nonNull, java.util.stream.Collectors.toList())));
69+
}
70+
71+
public static <T1, T2, T3, T4, T5, T6, T7, T8, T9>
72+
Collector<Tuple9<T1, T2, T3, T4, T5, T6, T7, T8, T9>, ?, Map<Tuple8<T1, T2, T3, T4, T5, T6, T7, T8>, List<T9>>> groupingBy8() {
73+
return java.util.stream.Collectors.groupingBy(
74+
t -> Tuple.of(t.getT1(), t.getT2(), t.getT3(), t.getT4(), t.getT5(), t.getT6(), t.getT7(), t.getT8()),
75+
LinkedHashMap::new,
76+
java.util.stream.Collectors.mapping(Tuple9::getT9, filtering(Objects::nonNull, java.util.stream.Collectors.toList())));
77+
}
78+
79+
80+
}

src/main/java/com/github/lokic/javaplus/package-info.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@
6969
* <li>{@link com.github.lokic.javaplus.functional.tuple.TuplePredicate3}
7070
* </ul>
7171
* <p>
72-
* {@link com.github.lokic.javaplus.Collectors} 相关扩展:
72+
* {@link com.github.lokic.javaplus.OtherCollectors} 相关扩展:
7373
* <ul>
74-
* <li>{@link com.github.lokic.javaplus.Collectors.Reversed} 倒序
75-
* <li>{@link com.github.lokic.javaplus.Collectors.Distinct} 去重
74+
* <li>{@link com.github.lokic.javaplus.OtherCollectors.Reversed} 倒序
75+
* <li>{@link com.github.lokic.javaplus.OtherCollectors.Distinct} 去重
7676
* </ul>
7777
* <p>
7878
* 其他:

src/test/java/com/github/lokic/javaplus/CollectorsTest.java renamed to src/test/java/com/github/lokic/javaplus/OtherCollectorsTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,44 @@
1212

1313
import static org.assertj.core.api.Assertions.entry;
1414

15-
public class CollectorsTest {
15+
public class OtherCollectorsTest {
1616

1717
@Test
1818
public void test_distinctFirstPut() {
1919
Assertions.assertThat(
2020
Stream.of("A", "B", "A", "C")
21-
.collect(Collectors.Distinct.distinctFirstPut()))
21+
.collect(OtherCollectors.Distinct.distinctFirstPut()))
2222
.containsExactly("A", "B", "C");
2323
}
2424

2525
@Test
2626
public void test_distinctLastPut() {
2727
Assertions.assertThat(
2828
Stream.of("A", "B", "A", "C")
29-
.collect(Collectors.Distinct.distinctLastPut()))
29+
.collect(OtherCollectors.Distinct.distinctLastPut()))
3030
.containsExactly("B", "A", "C");
3131
}
3232

3333
@Test
3434
public void test_distinctFirstPutByKey() {
3535
Assertions.assertThat(
3636
Stream.of(new DataInfo("A", "A1"), new DataInfo("B", "B1"), new DataInfo("A", "A2"), new DataInfo("C", "C1"))
37-
.collect(Collectors.Distinct.distinctFirstPutByKey(DataInfo::getKey)))
37+
.collect(OtherCollectors.Distinct.distinctFirstPutByKey(DataInfo::getKey)))
3838
.containsExactly(new DataInfo("A", "A1"), new DataInfo("B", "B1"), new DataInfo("C", "C1"));
3939
}
4040

4141
@Test
4242
public void test_distinctLastPutByKey() {
4343
Assertions.assertThat(
4444
Stream.of(new DataInfo("A", "A1"), new DataInfo("B", "B1"), new DataInfo("A", "A2"), new DataInfo("C", "C1"))
45-
.collect(Collectors.Distinct.distinctLastPutByKey(DataInfo::getKey)))
45+
.collect(OtherCollectors.Distinct.distinctLastPutByKey(DataInfo::getKey)))
4646
.containsExactly(new DataInfo("B", "B1"), new DataInfo("A", "A2"), new DataInfo("C", "C1"));
4747
}
4848

4949
@Test
5050
public void test_toMap() {
5151
Map<String, String> result = Stream.of(Tuple.of("A", "A1"), Tuple.of("B", "B1"), Tuple.of("A", "A2"), Tuple.of("C", "C1"))
52-
.collect(Collectors.toMap((k, v) -> k, (k, v) -> v, (a, b) -> a, HashMap::new));
52+
.collect(OtherCollectors.toMap((k, v) -> k, (k, v) -> v, (a, b) -> a, HashMap::new));
5353

5454
Assertions.assertThat(result)
5555
.containsExactly(entry("A", "A1"), entry("B", "B1"), entry("C", "C1"));
@@ -59,15 +59,15 @@ public void test_toMap() {
5959
public void test_toMapTupleStream() {
6060
Assertions.assertThat(
6161
Stream.of(Tuple.of("A", "A1"), Tuple.of("B", "B1"), Tuple.of("A", "A2"), Tuple.of("C", "C1"))
62-
.collect(Collectors.toMapTupleStream((k, v) -> k, (k, v) -> v, (a, b) -> a, HashMap::new))
62+
.collect(OtherCollectors.toMapTupleStream((k, v) -> k, (k, v) -> v, (a, b) -> a, HashMap::new))
6363
).containsExactly(Tuple.of("A", "A1"), Tuple.of("B", "B1"), Tuple.of("C", "C1"));
6464
}
6565

6666
@Test
6767
public void test_toMapEntryStream() {
6868
Assertions.assertThat(
6969
Stream.of(Tuple.of("A", "A1"), Tuple.of("B", "B1"), Tuple.of("A", "A2"), Tuple.of("C", "C1"))
70-
.collect(Collectors.toMapEntryStream((k, v) -> k, (k, v) -> v, (a, b) -> a, HashMap::new))
70+
.collect(OtherCollectors.toMapEntryStream((k, v) -> k, (k, v) -> v, (a, b) -> a, HashMap::new))
7171
).containsExactly(entry("A", "A1"), entry("B", "B1"), entry("C", "C1"));
7272
}
7373

0 commit comments

Comments
 (0)