Post

Java Stream Operations Cheatsheet

Java Stream Operations Cheatsheet

This is an evolving cheatsheet for common Java Stream operations.

groupingBy

Get the frequency map using Collectors.counting

1
2
3
4
5
6
7
import java.util.stream.Collectors;
import java.util.function.Function;

final List<String> letters = List.of("a", "b", "b", "c", "c", "c", "d", "d", "d", "d");

final Map<String, Long> frequencyMap = letters.stream()
  .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

Output: The frequencyMap will look like this: {a=1, b=2, c=3, d=4}

This post is licensed under CC BY 4.0 by the author.