Java Streams - Java Stream Statistics








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.

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;
// w  ww. j ava  2 s .  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.





Streams summary statistics

The summary statistics classes are designed to be used with streams.

They contain a combine() method that combines two summary statistics.

The following code shows how to compute the summary statistics for incomes.

import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.List;
/*  ww  w .j  a  v  a2  s .c  o m*/
public class Main {
  public static void main(String[] args) {
    DoubleSummaryStatistics incomeStats = Employee.persons()
        .stream()
        .map(Employee::getIncome)
        .collect(DoubleSummaryStatistics::new, 
                 DoubleSummaryStatistics::accept, 
                 DoubleSummaryStatistics::combine);
     System.out.println(incomeStats);
  }
}

class Employee {
  public static enum Gender {
    MALE, FEMALE
  }
  private long id;
  private String name;
  private Gender gender;
  private LocalDate dob;
  private double income;

  public Employee(long id, String name, Gender gender, LocalDate dob,
      double income) {
    this.id = id;
    this.name = name;
    this.gender = gender;
    this.dob = dob;
    this.income = income;
  }
  public double getIncome() {
    return income;
  }
  public static List<Employee> persons() {
    Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971,
        Month.JANUARY, 1), 2343.0);
    Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972,
        Month.JULY, 21), 7100.0);
    Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973,
        Month.MAY, 29), 5455.0);
    Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974,
        Month.OCTOBER, 16), 1800.0);
    Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975,
        Month.DECEMBER, 13), 1234.0);
    Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976,
        Month.JUNE, 9), 3211.0);
    List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6);
    return persons;
  }
}

The code above generates the following result.





Numeric Stream summary statistics

The Collectors class contains methods to compute the summary statistics for the specific type of numeric data.

  • Collectors.summarizingDouble() returns a DoubleSummaryStatistics.
  • Collectors.summarizingLong() returns a LongSummaryStatistics.
  • Collectors.summarizingInt() returns a IntSummaryStatistics.

The Collectors class contains methods such as counting(), summingDouble(), summingInt(), summingLong(), averagingDouble(), averagingLong(),averagingInt(), minBy(), and maxBy() that return a collector to perform a specific type of summary computation.

The following code shows how to calculate the summary statistics for income.

import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;
/*w ww  .  j  av a  2  s  .c o  m*/
public class Main {
  public static void main(String[] args) {
    DoubleSummaryStatistics incomeStats = Employee.persons()
        .stream()
        .collect(Collectors.summarizingDouble(Employee::getIncome));
    System.out.println(incomeStats);
  }
}

class Employee {
  public static enum Gender {
    MALE, FEMALE
  }

  private long id;
  private String name;
  private Gender gender;
  private LocalDate dob;
  private double income;

  public Employee(long id, String name, Gender gender, LocalDate dob,
      double income) {
    this.id = id;
    this.name = name;
    this.gender = gender;
    this.dob = dob;
    this.income = income;
  }
  public double getIncome() {
    return income;
  }
  public static List<Employee> persons() {
    Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971,
        Month.JANUARY, 1), 2343.0);
    Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972,
        Month.JULY, 21), 7100.0);
    Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973,
        Month.MAY, 29), 5455.0);
    Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974,
        Month.OCTOBER, 16), 1800.0);
    Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975,
        Month.DECEMBER, 13), 1234.0);
    Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976,
        Month.JUNE, 9), 3211.0);

    List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6);

    return persons;
  }
}

The code above generates the following result.