CKTIS / Java
Concurrent grouping and counting in Java
`Collectors.groupingByConcurrent` groups stream elements by key and can count each category without preserving the order in which elements are encountered.
Version: Java SE 21
Syntax
Declared in java.util.stream.Collectors:
public static <T, K> Collector<T, ?, ConcurrentMap<K, List<T>>> groupingByConcurrent(Function<? super T, ? extends K> classifier)
public static <T, K, A, D> Collector<T, ?, ConcurrentMap<K, D>> groupingByConcurrent(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)
public static <T, K, A, D, M extends ConcurrentMap<K, D>> Collector<T, ?, M> groupingByConcurrent(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)In Java SE 21, the static method Collectors.groupingByConcurrent in java.util.stream creates a collector for Stream.collect. The classifier function maps each input element of type T to a key of type K. The collector also works on a sequential stream and does not make the stream parallel by itself.
With just classifier, the result is a ConcurrentMap<K, List<T>>. With classifier, downstream, the downstream collector reduces each group's elements to a result of type D, giving a ConcurrentMap<K, D>. For example, Collectors.counting() gives a Long count for every key that occurs, without building lists of elements.
With classifier, mapFactory, downstream, the factory supplies a new, empty map of type M that implements ConcurrentMap<K, D>. For example, ConcurrentSkipListMap::new gives sorted key iteration, but does not restore input order within groups. Without a factory, the API does not guarantee the map's concrete implementation, mutability, or serializability. A concurrent map does not make the lists from the first overload thread-safe.
All three overloads are CONCURRENT and UNORDERED. On a parallel stream, collect can accumulate concurrently into a shared map even if the source is ordered; there is no need to call unordered(). The collector does not promise to preserve encounter order within a group.
Collectors.groupingBy is not a concurrent collector, but it is valid on a parallel stream: partial maps are built in isolation and then merged. Use it with an order-preserving downstream collector such as toList() when the order of elements within each group matters on an ordered stream. Concurrent grouping can avoid merging partial maps, but a speedup is not guaranteed. Measure with representative data.
Examples
import java.util.List;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> categories = List.of("book", "game", "book", "music", "game", "book");
ConcurrentMap<String, Long> counts = categories.parallelStream()
.collect(Collectors.groupingByConcurrent(
Function.identity(), Collectors.counting()));
System.out.println(new TreeMap<>(counts));
}
}Result
{book=3, game=2, music=1}`Function.identity()` uses the category string itself as the key. `Collectors.counting()` counts every occurrence. After `collect` finishes, a `TreeMap` copy sorts the keys for display only.
Related lessons
- Java · Lesson 43 · Advanced · Version: 2
Sources
- Collectors (Java SE 21 & JDK 21) — groupingByConcurrent(Function)
- Collectors (Java SE 21 & JDK 21) — groupingByConcurrent(Function, Collector)
- Collectors (Java SE 21 & JDK 21) — groupingByConcurrent(Function, Supplier, Collector)
- Collectors (Java SE 21 & JDK 21) — counting()
- Collectors (Java SE 21 & JDK 21) — groupingBy(Function, Collector)
- Stream (Java SE 21 & JDK 21) — collect(Collector)
- java.util.stream (Java SE 21 & JDK 21) — Reduction, concurrency, and ordering