Java Reference ยท Streams and Functional
Collectors counting(), summing and averaging
Reference for the aggregating Collectors in Java: counting, summingInt/Long/Double, averagingInt/Long/Double, summarizing and min/max, with return types.
The Collectors class has a family of collectors that boil a whole stream, or a whole group, down to one number: counting(), summingInt(), summingLong(), summingDouble(), averagingInt(), averagingLong(), averagingDouble(), and the summarizing...() versions that return several statistics at once.
They are most useful as downstream collectors inside groupingBy() or partitioningBy(), where they replace the SQL COUNT, SUM and AVG that you would write after a GROUP BY. Used on their own, a primitive stream method such as IntStream.sum() is usually simpler.
long n = people.stream().collect(Collectors.counting()); int totalAge = people.stream().collect(Collectors.summingInt(Person::age)); double mean = people.stream().collect(Collectors.averagingInt(Person::age));
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
static <T> Collector<T, ?, Long> counting() static <T> Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper) static <T> Collector<T, ?, Long> summingLong(ToLongFunction<? super T> mapper) static <T> Collector<T, ?, Double> summingDouble(ToDoubleFunction<? super T> mapper) static <T> Collector<T, ?, Double> averagingInt(ToIntFunction<? super T> mapper) static <T> Collector<T, ?, Double> averagingLong(ToLongFunction<? super T> mapper) static <T> Collector<T, ?, Double> averagingDouble(ToDoubleFunction<? super T> mapper) static <T> Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper) static <T> Collector<T, ?, Optional<T>> maxBy(Comparator<? super T> comparator) static <T> Collector<T, ?, Optional<T>> minBy(Comparator<? super T> comparator)
| Collector | Result type | Empty input |
|---|---|---|
counting() |
Long |
0 |
summingInt |
Integer |
0 |
summingLong |
Long |
0 |
summingDouble |
Double |
0.0 |
averaging... |
Double |
0.0 |
maxBy, minBy |
Optional<T> |
Optional.empty() |
Usage notes
- Return types differ:
counting()gives aLong,summingInt()anInteger, everyaveraging...()aDouble. Unboxing aLonginto anintneeds an explicit cast. - Overflow:
summingInt()wraps around silently on overflow. UsesummingLong()for large totals. - Average of nothing is
0.0, not an error and notNaN. Check the count if "no data" must be distinguished. - Floating point:
summingDouble()andaveragingDouble()use compensated summation, so they are more accurate than a naive loop, but results still carry rounding error. UseBigDecimalandreduce()for money. - Shortcuts:
stream.count()is simpler thancollect(counting()), and on primitive streams usesum(),average()andsummaryStatistics(). teeing()combines two of these in one pass, such as sum and count into an average you compute yourself; seecollectors-teeing.- Null mapper results: the mapper returns a primitive, so there is no null to worry about, but a null element passed to a method reference will throw
NullPointerException.