The 30 second answer
I use summarizingDouble so sum, average, count, min, and max all come from one pass. Floating-point sums can drift with order or parallelism, so for money I switch to a BigDecimal reduce instead of double.
DoubleSummaryStatistics s = emps.stream()
.collect(Collectors.summarizingDouble(Employee::salary));
s.getSum();
s.getAverage();One pass. Floating-point sum can differ by encounter order or parallelism.
[Opinion] For money use BigDecimal with reduce(BigDecimal.ZERO, BigDecimal::add).
Sources:
Sources (0)
See inline citations in the answer above.