CKTIS / Java
Collectors: summary statistics and concurrent grouping
The java.util.stream.Collectors class supplies collectors for Stream.collect. Combine Collectors.groupingByConcurrent with Collectors.summarizingDouble to calculate numeric summary statistics separately for each group.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
stream.collect(Collectors.summarizingDouble(mapper))
stream.collect(Collectors.groupingByConcurrent(classifier, Collectors.summarizingDouble(mapper)))java.util.stream.Collectors supplies reusable reduction strategies for the terminal operation Stream.collect. Collectors compose: an outer collector forms groups, while a downstream collector calculates each group's result. This entry targets Java SE 21; the production runtime is not attested.
Collectors.summarizingDouble(mapper) returns a Collector<T, ?, DoubleSummaryStatistics>. When used with Stream.collect, the collector applies mapper to each element to obtain a double value. The result of collect is a DoubleSummaryStatistics object containing the count, sum, minimum, maximum, and average. For empty input, count, sum, and average are zero, minimum is Double.POSITIVE_INFINITY, and maximum is Double.NEGATIVE_INFINITY. Rounding can change the sum and average; NaN values affect the statistics.
Collectors.groupingByConcurrent(classifier) and the overload with a downstream collector both return collectors. Using the first with Stream.collect produces a ConcurrentMap<K, List<T>>; using the second produces a ConcurrentMap<K, D>. Both collectors are concurrent and have the UNORDERED characteristic: they do not guarantee preservation of encounter order, but they can still preserve it. For example, sequential collection can retain encounter order within each group's list. In contrast, groupingBy is not a concurrent collector. The resulting lists themselves have no thread-safety guarantee.
Concurrent reduction requires a parallel stream; the collector alone does not start one. Functions should be stateless and must not modify the source. A concurrent map does not make subsequent mutations of its DoubleSummaryStatistics values thread-safe. The example sorts keys only before printing; its small, exactly representable values make the statistics predictable.
Examples
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Locale;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
public class Main {
record Item(String category, double value) {}
public static void main(String[] args) {
var items = List.of(
new Item("A", 2.0),
new Item("B", 8.0),
new Item("A", 4.0)
);
ConcurrentMap<String, DoubleSummaryStatistics> stats =
items.parallelStream().collect(Collectors.groupingByConcurrent(
Item::category,
Collectors.summarizingDouble(Item::value)
));
new TreeMap<>(stats).forEach((category, s) ->
System.out.printf(Locale.ROOT,
"%s: count=%d sum=%.1f min=%.1f max=%.1f avg=%.1f%n",
category, s.getCount(), s.getSum(), s.getMin(),
s.getMax(), s.getAverage()));
}
}Result
A: count=2 sum=6.0 min=2.0 max=4.0 avg=3.0 B: count=1 sum=8.0 min=8.0 max=8.0 avg=8.0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.