JavaIntermediate#streams

How do you group elements using Collectors.groupingBy?

groupingBy() classifies stream elements into a Map by a classifier function, with an optional downstream collector to further aggregate each group's values (counting, summing, mapping, etc.).

Example
Map<Integer, List<String>> byLength = Stream.of("a","bb","cc","ddd")
  .collect(Collectors.groupingBy(String::length));
// {1=[a], 2=[bb, cc], 3=[ddd]}

Related Questions

1
JavaIntermediate#streams

What is the difference between findFirst() and findAny() in streams?

Open
2
JavaBeginner#java8

What is the difference between Optional.of() and Optional.ofNullable()?

Open
3
JavaIntermediate#java8#lambda

What is a method reference and what are its types?

Open