Numeric Java streams provide summary statistics
The numeric streams (IntStream
, LongStream
, DoubleStream
, more specifically their pipeline implementations) have a #summaryStatistics
static method, which provides some statistics for the stream content.
For example, with IntStream
you’ll get a IntSummaryStatistics
instance:
var numbers = IntStream.iterate(1, (i) -> i + 1).limit(100);
var statistics = numbers.summaryStatistics();
System.out.println(String.format("Average: %f", statistics.getAverage()));
System.out.println(String.format("Sum: %d", statistics.getSum()));
System.out.println(String.format("Min: %d", statistics.getMin()));
System.out.println(String.format("Max: %d", statistics.getMax()));
That would output:
Average: 50.500000
Sum: 5050
Min: 1
Max: 100
Although fairly trivial, these utilities make your life a little bit happier :)