2
2
3
3
import java .util .HashMap ;
4
4
import java .util .Map ;
5
+ import java .util .function .BiConsumer ;
6
+ import java .util .function .BinaryOperator ;
7
+ import java .util .function .Function ;
8
+ import java .util .function .Supplier ;
9
+ import java .util .stream .Collector ;
5
10
import java .util .stream .Collectors ;
6
11
import java .util .stream .LongStream ;
7
12
@@ -51,10 +56,11 @@ public void addAll(LongCountMap<K> amountPerCloneClassSize) {
51
56
amountPerCloneClassSize .entrySet ().stream ().forEach (e -> this .increment (e .getKey (), e .getValue ()));
52
57
}
53
58
54
- public void incrementAll (LongCountMap <K > input ) {
59
+ public LongCountMap < K > incrementAll (LongCountMap <K > input ) {
55
60
for (Entry <K , Long > i : input .entrySet ()) {
56
61
increment (i .getKey (), i .getValue ());
57
62
}
63
+ return this ;
58
64
}
59
65
60
66
public long sumValues () {
@@ -66,4 +72,12 @@ public static LongCountMap<Long> ofFrequencies(LongStream frequencies) {
66
72
frequencies .forEach (lcm ::increment );
67
73
return lcm ;
68
74
}
75
+
76
+ public static <T > Collector <T , LongCountMap <T >, LongCountMap <T >> toCountMap () {
77
+ final Supplier <LongCountMap <T >> supplier = LongCountMap ::new ;
78
+ final BiConsumer <LongCountMap <T >, T > accumulator = LongCountMap ::increment ;
79
+ final BinaryOperator <LongCountMap <T >> combiner = LongCountMap ::incrementAll ;
80
+ final Function <LongCountMap <T >, LongCountMap <T >> finisher = LongCountMap ::new ;
81
+ return Collector .of (supplier , accumulator , combiner , finisher );
82
+ }
69
83
}
0 commit comments