Example usage for java.util.stream Collectors groupingByConcurrent

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

Introduction

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

Prototype

public static <T, K> Collector<T, ?, ConcurrentMap<K, List<T>>> groupingByConcurrent(
        Function<? super T, ? extends K> classifier) 

Source Link

Document

Returns a concurrent Collector implementing a "group by" operation on input elements of type T , grouping elements according to a classification function.

Usage

From source file:Main.java

public static void main(String[] args) {
    List<String> strings = new LinkedList<>();
    strings.add("a");
    strings.add("B");
    strings.add("ab");
    strings.add("abc");
    strings.add("ABC");

    ConcurrentMap<Integer, List<String>> byLength = strings.parallelStream()
            .collect(Collectors.groupingByConcurrent(String::length));

    System.out.println(byLength);
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> roster = createRoster();
    ConcurrentMap<Person.Sex, List<Person>> byGenderParallel = roster.parallelStream()
            .collect(Collectors.groupingByConcurrent(Person::getGender));

    List<Map.Entry<Person.Sex, List<Person>>> byGenderList = new ArrayList<>(byGenderParallel.entrySet());

    System.out.println("Group members by gender:");
    byGenderList.stream().forEach(e -> {
        System.out.println("Gender: " + e.getKey());
        e.getValue().stream().map(Person::getName).forEach(f -> System.out.println(f));
    });/*from   ww  w  .  j av a2s.c  om*/

}