DoubleSummaryStatistics Example

DoubleSummaryStatistics is an object for collecting statistics such as count, min, max, sum, and average. Although not required, it is designed to work with streams. If you are working with longs use LongSummaryStatistics or ints use IntSummaryStatistics .

Setup

class Company {

    double revenue;

    public Company(double revenue) {
        super();
        this.revenue = revenue;
    }

    public double getRevenue() {
        return revenue;
    }
}

List<Company> companies;

@Before
public void setUp() {
    companies = new ArrayList<>();
    companies.add(new Company(100.12));
    companies.add(new Company(142.65));
    companies.add(new Company(12.1));
    companies.add(new Company(184.90));
}

Java 8

Compute summary statistics on a stream

@Test
public void double_summary_stats_with_stream() {

    DoubleSummaryStatistics stats = companies.stream()
            .mapToDouble((x) -> x.getRevenue()).summaryStatistics();

    // average
    assertEquals(109.9425, stats.getAverage(), 0);

    // count
    assertEquals(4, stats.getCount(), 0);

    // max
    assertEquals(184.9, stats.getMax(), 0);

    // min
    assertEquals(12.1, stats.getMin(), 0);

    // sum
    assertEquals(439.77, stats.getSum(), 0);
}

DoubleSummaryStatistics as a reduction target

@Test
public void double_summary_stats_stream_reduction_target() {

    DoubleSummaryStatistics stats = companies.stream().collect(
            Collectors.summarizingDouble(Company::getRevenue));

    // average
    assertEquals(109.9425, stats.getAverage(), 0);

    // count
    assertEquals(4, stats.getCount(), 0);

    // max
    assertEquals(184.9, stats.getMax(), 0);

    // min
    assertEquals(12.1, stats.getMin(), 0);

    // sum
    assertEquals(439.77, stats.getSum(), 0);
}