Example usage for java.util HashMap compute

List of usage examples for java.util HashMap compute

Introduction

In this page you can find the example usage for java.util HashMap compute.

Prototype

@Override
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) 

Source Link

Document

This method will, on a best-effort basis, throw a ConcurrentModificationException if it is detected that the remapping function modifies this map during computation.

Usage

From source file:org.briljantframework.data.Collectors.java

public static <T> Collector<T, ?, T> mode() {
    return Collector.of(HashMap::new, (HashMap<T, Integer> map, T value) -> map.compute(value,
            (key, count) -> count == null ? 1 : count + 1), (left, right) -> {
                right.forEach((k, v) -> left.merge(k, v, (Integer o, Integer n) -> o == null ? n : o + n));
                return left;
            }, (HashMap<T, Integer> map) -> {
                int max = 0;
                T value = null;/*ww  w. j  a va 2 s.c o  m*/
                for (Map.Entry<T, Integer> k : map.entrySet()) {
                    if (k.getValue() > max) {
                        value = k.getKey();
                    }
                }
                return value;
            }, Collector.Characteristics.UNORDERED);
}