Example usage for java.util.stream Collectors toConcurrentMap

List of usage examples for java.util.stream Collectors toConcurrentMap

Introduction

In this page you can find the example usage for java.util.stream Collectors toConcurrentMap.

Prototype

public static <T, K, U> Collector<T, ?, ConcurrentMap<K, U>> toConcurrentMap(
        Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper,
        BinaryOperator<U> mergeFunction) 

Source Link

Document

Returns a concurrent Collector that accumulates elements into a ConcurrentMap whose keys and values are the result of applying the provided mapping functions to the input elements.

Usage

From source file:Main.java

public static void main(String[] args) {
    Map<Employee.Gender, Long> countByGender = Employee.persons().stream().collect(Collectors
            .toConcurrentMap(Employee::getGender, p -> 1L, (oldCount, newCount) -> newCount + oldCount));

    System.out.println(countByGender);
}

From source file:Main.java

public static void main(String[] args) {
    List<String> terms = Arrays.asList("this", "is", "is", "a");

    Map<String, Integer> result = terms.parallelStream().flatMap(s -> Arrays.asList(s.split(" ")).stream())
            .collect(Collectors.toConcurrentMap(w -> w.toLowerCase(), w -> 1, Integer::sum));
    System.out.println(result);//from w w  w  .  j  av  a 2 s . c o m
}

From source file:ru.anr.base.BaseParent.java

/**
 * A short-cut function to simplify the process of building a map from a
 * collection when there can be non-unique keys.
 * /* www  . jav a  2s .  c  o m*/
 * @param collection
 *            An original collection
 * @param keyMapper
 *            A key mapper
 * @param valueMapper
 *            A value mapper
 * @param mergeFunction
 *            A function to merge values conflicting by their key
 * @return A new map
 * 
 * @param <T>
 *            The type of a collection item
 * @param <K>
 *            Type of the map key
 * @param <U>
 *            Type of the map value
 */
public static <T, K, U> Map<K, U> toMap(Collection<T> collection, Function<? super T, ? extends K> keyMapper,
        Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction) {

    return collection.stream().collect(Collectors.toConcurrentMap(keyMapper, valueMapper, mergeFunction));
}