Java Snippet

Group by Key with Stream Collectors

Difficulty: Medium

`Collectors.groupingBy` is the Java equivalent of SQL `GROUP BY`: pass a key extractor and you get back a `Map<K, List<T>>`. This snippet covers the basic grouping, downstream collectors (counting, summing, mapping to a different value), and multi-level grouping by chaining two `groupingBy` calls. Pair with `LinkedHashMap` when you need stable insertion order.