Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class MostPopular<T> implements Collector<T, Map<T, Integer>, Optional<T>> {
- @Override
- public Supplier<Map<T, Integer>> supplier() {
- return HashMap::new;
- }
- @Override
- public BiConsumer<Map<T, Integer>, T> accumulator() {
- return (acc, elem) -> {
- Integer value = acc.get(elem);
- if (value == null) {
- value = 1;
- } else {
- value += 1;
- }
- acc.put(elem, value);
- };
- }
- @Override
- public BinaryOperator<Map<T, Integer>> combiner() {
- return (acc1, acc2) -> {
- throw new UnsupportedOperationException();
- };
- }
- @Override
- public Function<Map<T, Integer>, Optional<T>> finisher() {
- return (acc) -> acc.entrySet().stream()
- .reduce((a, b) -> a.getValue() > b.getValue() ? a : b)
- .map(Map.Entry::getKey);
- }
- @Override
- public Set<Characteristics> characteristics() {
- return Collections.emptySet();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement