The groupingBy() method in Java streams groups elements by a specified property, similar to SQL's GROUP BY clause. It collects elements into a Map, with keys as the grouping criteria and values as lists or aggregated results.
- Group elements based on a specified property.
- Returns a Map of key, list/aggregated values.
- Supports aggregations like counting, summing, or averaging
import java.util.*;
import java.util.stream.Collectors;
public class GFG{
public static void main(String[] args){
List<Integer> numbers = Arrays.asList(10, 20, 10, 30, 20, 10);
// Group numbers by their value
Map<Integer, Long> frequencyMap = numbers.stream()
.collect(Collectors.groupingBy(
// classifier: group by number itself
n -> n,
// downstream: count occurrences
Collectors.counting()
));
System.out.println(frequencyMap);
}
}
Output
{20=2, 10=3, 30=1}
Explanation:
- Groups numbers based on their own value.
- Uses Collectors.counting() to count the occurrences.
- Returns a Map<Integer, Long>, where the key is the number, and the value is its frequency.
- Demonstrates the second overload of groupingBy() (with downstream collector).
Variants of groupingBy() Method
The groupingBy() method is overloaded three times in Java:
1. Simple groupingBy
Group Elements into a List
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class GFG{
public static void main(String[] args){
List<String> words = Arrays.asList("geeks", "for", "geeks");
Map<String, List<String>> result = words.stream()
.collect(Collectors.groupingBy(Function.identity()));
System.out.println(result);
}
}
Output
{geeks=[geeks, geeks], for=[for]}
Explanation:
- Groups words by their value.
- Values are collected as lists of elements.
- Uses the simplest overload of groupingBy().
- Useful when you need all grouped elements, not aggregated results.
Syntax:
public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(
Function<? super T, ? extends K> classifier
)
Parameters:
- T: Type of input elements
- K: Type of key for grouping
- classifier: Function to extract the key from elements
2. groupingBy with Downstream Collector
Aggregate Elements per Key
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class GFG{
public static void main(String[] args){
List<String> words
= Arrays.asList("geeks", "for", "geeks");
Map<String, Long> result = words.stream().collect(
Collectors.groupingBy(Function.identity(),
Collectors.counting()));
System.out.println(result);
}
}
Output
{geeks=2, for=1}
Explanation:
- Groups words by their value.
- Uses Collectors.counting() to count frequency of each key.
- Returns a Map<String, Long>.
- Allows aggregation instead of collecting elements into a list.
Syntax:
public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(
Function<? super T, ? extends K> classifier,
Collector<? super T, A, D> downstream
)
Parameters:
- T: Type of input elements.
- K: Type of key for grouping.
- A: Intermediate accumulation type of downstream collector.
- D: Final result type of downstream collector.
- classifier: Function to extract the key.
- downstream: Collector to perform aggregation on grouped elements.
3. groupingBy with Map Supplier and Downstream Collector
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class GFG {
public static void main(String[] args)
{
List<String> words
= Arrays.asList("geeks", "for", "geeks");
Map<String, Long> result
= words.stream().collect(Collectors.groupingBy(
Function.identity(), TreeMap::new,
Collectors.counting()));
System.out.println(result);
}
}
Output
{for=1, geeks=2}
Explanation:
- Groups words by value.
- Uses TreeMap to store keys in sorted order.
- Aggregates values using Collectors.counting().
- Returns a custom map type, useful when key order matters.
Syntax:
public static <T, K, D, M extends Map<K,D>> Collector<T, ?, M> groupingBy(
Function<? super T, ? extends K> classifier,
Supplier<M> mapFactory,
Collector<? super T, ?, D> downstream
)
Parameters:
- T: Type of input elements.
- K: Type of key for grouping.
- D: Type of result of downstream collector.
- M: Type of Map to create.
- classifier: Function to extract key.
- mapFactory: Supplier to create a Map (e.g., TreeMap::new).
- downstream: Collector to perform aggregation.