Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
values
double
statistics

How to use DoubleSummaryStatistics in Java

abhilash

When to use DoubleSummaryStatistics

For double values, DoubleSummaryStatistics is used to collect counts, min, max, total, and average statistics.

This class is intended to operate with streams, although it is not required. It was introduced in Java 8.

How to import DoubleSummaryStatistics

DoubleSummaryStatistics is defined in the util package in Java, so you must import the util package before using the DoubleSummaryStatistics class, as shown below:


import java.util.DoubleSummaryStatistics;

The statistics offered by this class are as follows:

  1. Count of all elements.
  2. Cumulative sum of the elements.
  3. Minimum element.
  4. Maximum element.
  5. Average of all the elements.

Methods

Method Name Description
accept(double value) Another value is added to the summary information.
combine(DoubleSummaryStatistics other) The state of another DoubleSummaryStatistics object is merged using this method
getCount() Returns the total number of values that have been seen so far.
getSum() Returns the sum of all values observed so far, or zero if none have been seen
getMin() Returns the smallest recorded value
getMax() Returns the largest recorded value
getAverage() Returns the arithmetic mean of all values seen so far, or zero if none have been seen.

Code

Example 1

In the code below, we are computing the statistics from a list of double values. The accept method is used to take the value into the summary information:

import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;

public class Main {

    public static void main(String args[])
    {
        List<Double> doubleList = new ArrayList<>();
        doubleList.add(12.4);
        doubleList.add(100.0);
        doubleList.add(136.5);
        doubleList.add(1000.2345);
        doubleList.add(200.34);
        doubleList.add(1.1);

        DoubleSummaryStatistics doubleSummaryStatistics = new DoubleSummaryStatistics();
        for(double d: doubleList) doubleSummaryStatistics.accept(d);

        System.out.println("Count of the elements - " + doubleSummaryStatistics.getCount());
        System.out.println("Sum of the elements - " + doubleSummaryStatistics.getSum());
        System.out.println("Minimum element - " + doubleSummaryStatistics.getMin());
        System.out.println("Maximum element - " + doubleSummaryStatistics.getMax());
        System.out.println("Average of the elements - " + doubleSummaryStatistics.getAverage());

    }
}

Example 2

In the code below, we use streams to get summary statistics:

import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    public static void main(String args[])
    {
        List<Double> doubleList = new ArrayList<>();
        doubleList.add(12.4);
        doubleList.add(100.0);
        doubleList.add(136.5);
        doubleList.add(1000.2345);
        doubleList.add(200.34);
        doubleList.add(1.1);

        DoubleSummaryStatistics doubleSummaryStatistics = doubleList.stream().collect(Collectors.summarizingDouble(e -> e));

        System.out.println("Count of the elements - " + doubleSummaryStatistics.getCount());
        System.out.println("Sum of the elements - " + doubleSummaryStatistics.getSum());
        System.out.println("Minimum element - " + doubleSummaryStatistics.getMin());
        System.out.println("Maximum element - " + doubleSummaryStatistics.getMax());
        System.out.println("Average of the elements - " + doubleSummaryStatistics.getAverage());

    }
}

RELATED TAGS

java
communitycreator
values
double
statistics
RELATED COURSES

View all Courses

Keep Exploring