Summary Statistics

Description

The java.util package contains three classes to collect statistics:

  • DoubleSummaryStatistics
  • LongSummaryStatistics
  • IntSummaryStatistics

We can use them to compute the summary statistics on any group of numeric data.

Example

The following code shows how to compute the statistics on a number of double values.


import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.List;
/*from w  w w.j av a  2s  .  c o  m*/
public class Main {
  public static void main(String[] args) {
    DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
    stats.accept(100.0);
    stats.accept(300.0);
    stats.accept(230.0);
    stats.accept(532.0);
    stats.accept(422.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);
  }
}

The code above generates the following result.





















Home »
  Java Streams »
    Tutorial »




Java Streams Tutorial