Java - Stream Summary Statistics

Introduction

To get the maximum, minimum, sum, average, and count of data, use the statistics classes.

java.util package contains three classes to collect statistics:

DoubleSummaryStatistics
LongSummaryStatistics
IntSummaryStatistics

The following code shows how to DoubleSummaryStatistics.

It computes Summary Statistics on a Group of Numeric Data.

Demo

import java.util.DoubleSummaryStatistics;

public class Main {
  public static void main(String[] args) {
    DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
    stats.accept(100.0);//from  w w w .  j  a  va  2s  .co m
    stats.accept(500.0);
    stats.accept(400.0);

    long count = stats.getCount();
    double sum = stats.getSum();
    double min = stats.getMin();
    double avg = stats.getAverage();
    double max = stats.getMax();

    System.out.printf("count=%d, sum=%.2f, min=%.2f, average=%.2f, max=%.2f%n", count, sum, min, max, avg);

  }
}

Result

Related Topics