Collectors: groupingBy, counting, joining

Collect a stream into a map of groups, count each group, or join strings with separators.

Java · intermediate · 8 min · 29 steps · free

What you'll learn

CollectorsDemo.java

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class CollectorsDemo {
    public static void main(String[] args) {
        List<String> words = List.of("apple", "avocado", "banana", "blueberry", "cherry");

        Map<Character, List<String>> byLetter = words.stream()
                .collect(Collectors.groupingBy(w -> w.charAt(0), TreeMap::new, Collectors.toList()));
        Map<Character, Long> counts = words.stream()
                .collect(Collectors.groupingBy(w -> w.charAt(0), TreeMap::new, Collectors.counting()));
        String joined = words.stream()
                .filter(w -> w.length() <= 6)
                .collect(Collectors.joining(", ", "[", "]"));

        System.out.println(byLetter);
        System.out.println(counts + " " + joined);
    }
}

Step by step

  1. Five fruit names.
  2. groupingBy sorts items into groups by a key: here, the first letter.
  3. Java calls lambda$main$0(), with w = "apple". It gets its own variables on the stack.
  4. The key lambda runs once per word.
  5. w changes from "avocado" to "banana".
  6. w changes from "banana" to "blueberry".
  7. w changes from "blueberry" to "cherry".
  8. lambda$main$0() has finished, so its variables are gone.
  9. The value it returned, {a=[apple, avocado], b=[banana, blueberry], c=[cherry]}, is stored in byLetter.
  10. The same grouping, but counting() summarises each group as a number.
  11. Java calls lambda$main$1(), with w = "apple". It gets its own variables on the stack.
  12. w changes from "apple" to "avocado".
  13. w changes from "avocado" to "banana".
  14. w changes from "banana" to "blueberry".
  15. w changes from "blueberry" to "cherry".
  16. lambda$main$1() has finished, so its variables are gone.
  17. The value it returned, {a=2, b=2, c=1}, is stored in counts.
  18. joining glues the strings together with ", " between and [ ] around.
  19. Line runs: .filter(w -> w.length() <= 6).
  20. Java calls lambda$main$2(), with w = "apple". It gets its own variables on the stack.
  21. w changes from "apple" to "avocado".
  22. w changes from "avocado" to "banana".
  23. w changes from "banana" to "blueberry".
  24. w changes from "blueberry" to "cherry".
  25. lambda$main$2() has finished, so its variables are gone.
  26. The value it returned, "[apple, banana, cherry]", is stored in joined.
  27. It prints {a=[apple, avocado], b=[banana, blueberry], c=[cherry]}.
  28. It prints {a=2, b=2, c=1} [apple, banana, cherry].
  29. Groups, counts and a joined string, each made by a different collector at the end of a stream.

Common mistakes

Best practices

Key takeaways

Check yourself

What does Collectors.counting() produce for each group?

Answer: A Long count. It counts.

Stream.of("a","b").collect(joining("-"))?

Answer: "a-b". The separator goes between items.

·

More in The Stream API