-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathCollectorCombination.java
More file actions
executable file
·65 lines (50 loc) · 2.03 KB
/
CollectorCombination.java
File metadata and controls
executable file
·65 lines (50 loc) · 2.03 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
package part3.exercise;
import org.junit.Test;
import part2.exercise.CollectorsExercise2;
import part2.exercise.CollectorsExercise2.Key;
import part2.exercise.CollectorsExercise2.Value;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collector;
import static java.util.stream.Collectors.*;
public class CollectorCombination {
private static class Pair<A, B> {
private final A a;
private final B b;
public Pair(A a, B b) {
this.a = a;
this.b = b;
}
public A getA() {
return a;
}
public B getB() {
return b;
}
}
private static <T, M1, M2, R1, R2> Collector<T, Pair<M1, M2>, Pair<R1, R2>> paired(Collector<T, M1, R1> c1,
Collector<T, M2, R2> c2) {
// TODO
throw new UnsupportedOperationException();
}
@Test
public void collectKeyValueMap() {
// TODO see CollectorsExercise1::collectKeyValueMap
// В 1 проход в 2 Map с использованием MapPair и mapMerger
// final MapPair res2 = pairs.stream()
// .collect(new Collector<Pair, MapPair, MapPair>() {
// Перепишите решение в слещующем виде:
final Set<CollectorsExercise2.Pair> pairs = CollectorsExercise2.generatePairs(10, 100);
final Pair<Map<String, Key>, Map<String, List<Value>>> res2 = pairs.stream()
.collect(
paired(
mapping(CollectorsExercise2.Pair::getKey, toMap(Key::getId, Function.identity(), (x, y) -> x)),
mapping(CollectorsExercise2.Pair::getValue, groupingBy(Value::getKeyId))
)
);
// TODO tests
throw new UnsupportedOperationException();
}
}