Skip to content
Tutorials Center
My learning

Topics

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.

Java
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

Java Counting, summing, averaging

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.

Java Per-group statistics

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

Java
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 a Long, summingInt() an Integer, every averaging...() a Double. Unboxing a Long into an int needs an explicit cast.
  • Overflow: summingInt() wraps around silently on overflow. Use summingLong() for large totals.
  • Average of nothing is 0.0, not an error and not NaN. Check the count if "no data" must be distinguished.
  • Floating point: summingDouble() and averagingDouble() use compensated summation, so they are more accurate than a naive loop, but results still carry rounding error. Use BigDecimal and reduce() for money.
  • Shortcuts: stream.count() is simpler than collect(counting()), and on primitive streams use sum(), average() and summaryStatistics().
  • teeing() combines two of these in one pass, such as sum and count into an average you compute yourself; see collectors-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.