Skip to content

Commit 9740741

Browse files
committed
toCountMap
1 parent 7d43652 commit 9740741

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/main/java/com/sbaars/adventofcode/year19/util/LongCountMap.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import java.util.HashMap;
44
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;
510
import java.util.stream.Collectors;
611
import java.util.stream.LongStream;
712

@@ -51,10 +56,11 @@ public void addAll(LongCountMap<K> amountPerCloneClassSize) {
5156
amountPerCloneClassSize.entrySet().stream().forEach(e -> this.increment(e.getKey(), e.getValue()));
5257
}
5358

54-
public void incrementAll(LongCountMap<K> input) {
59+
public LongCountMap<K> incrementAll(LongCountMap<K> input) {
5560
for (Entry<K, Long> i : input.entrySet()) {
5661
increment(i.getKey(), i.getValue());
5762
}
63+
return this;
5864
}
5965

6066
public long sumValues() {
@@ -66,4 +72,12 @@ public static LongCountMap<Long> ofFrequencies(LongStream frequencies) {
6672
frequencies.forEach(lcm::increment);
6773
return lcm;
6874
}
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+
}
6983
}

0 commit comments

Comments
 (0)