Collectors.teeing (Java 12) feeds every element to two collectors and merges the results, so you do not have to stream the source twice.
var prices = List.of(120, 80, 250, 60, 190);
record Summary(long count, int total) {}
Summary s = prices.stream().collect(Collectors.teeing(
Collectors.counting(),
Collectors.summingInt(Integer::intValue),
Summary::new));
System.out.println(s);
System.out.println("average = " + (s.total() / s.count()));
Summary[count=5, total=700]
average = 140
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-30