Saturday, August 25, 2018

java8 - java.util.DoubleSummaryStatistics class example

In my previous post, we discussed Introduction to Summary Statistics in java8.
In this blog post, We are going to explore the DoubleSummaryStatistics class and methods with examples.

Java8 DoubleSummaryStatistics class 

java8 - java.util.DoubleSummaryStatistics class example
DoubleSummaryStatistics is one of the clas for creating state object for calculating statistical operations like sum, max, min, average, and count of a double data.
DoubleSummaryStatistics is for Double datatype defined in java.util package. There are other classes like IntSummaryStatistics for integer data type and LongSummaryStatistics for the long datatype.

These objects can be created and used in many ways. Following are the ways of getting this objects.

DoubleSummaryStatistics accept method example 

The below code is to calculate the summary statistics of numeric double values. This is one of the ways of getting summary statics data in java.
Here accept() method is used to add the values for computing statistical values. This takes Double as a parameter.

DoubleSummaryStatistics statsPrimitive = new DoubleSummaryStatistics();
The output of the above code
DoubleSummaryStatistics{count=5, sum=85.000000, min=2.000000, average=17.000000, max=50.000000} 

DoubleSummaryStatistics Object example

First Create a List using of() factor static method which was introduced in java9. This is one of the ways to create an unmodified collection for limited data set and returns list. Please see List.of() method Create a stream from List of Objets using List.Stream() method. Applies given object stream to mapToDouble() method which takes ToDoubleFunction as an input parameter. Please click ToDoubleFunction functional interface for more information. Each element of a stream is mapped and converted to Double value using Lambda expression Finally calling summarystatistics() method to return the state object for statistical data.
import java.util.DoubleSummaryStatistics;
import java.util.List;
public class Summary2 {
 public static void main(String[] args) {
  List<Employee> empLists=List.of(new Employee(5000),new Employee(2000),new Employee(21000),new Employee(2500));
     DoubleSummaryStatistics sumaryStats =
             .mapToDouble((value) -> value.getSalary()).summaryStatistics();
class Employee {

    double salary;

    public Employee(double salary) {
        this.salary = salary;

    public double getSalary() {
        return salary;
the output of the above code is
DoubleSummaryStatistics{count=4, sum=30500.000000, min=2000.000000, average=7625.000000, max=21000.000000}

DoubleStream summaryStatistics() method example 

There is another way to calculate statistics using DoubleStream summaryStatistics() method
DoubleStream doubleStream = DoubleStream.of(5.0, 4.0, 1.0, 7.0, 3.0);
DoubleSummaryStatistics summaryStats = doubleStream.summaryStatistics();
Output is
DoubleSummaryStatistics{count=5, sum=20.000000, min=1.000000, average=4.000000, max=7.000000}
First, create a double stream using DoubleStream.of() method. DoubleStream is a Stream class for doing Sequential and parallel operations on primitive double data. It is double version Stream class in java8. Calling doubleStream.summaryStatistics() method to get DoubleSummaryStatistics object which has summary data like count,sum,min,average and max of double data. This is reduction operation of Stream API and takes input array of values and gives an output of summary of data.

Collectors.summarizingDouble() method example 

Collectors.summarizingDouble() is a static method defined in package. Syntax
<T> Collector<T,?,DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)
the summarizingDouble method takes ToDoubleFunction as an input parameter.ToDoubleFunction is a functional interface provided in java.util.function package. These functional interfaces convert objects to a primitive double. Please see more about ToDoubleFunction  It returns which convert each element to Double by mapping each element to Function and returns DoubleSummaryStatistics class
List<Double> list = Arrays.asList(5.0, 4.0, 1.0, 7.0, 3.0);
DoubleSummaryStatistics summaryStats1 =;
The output of the above code is
DoubleSummaryStatistics{count=5, sum=20.000000, min=1.000000, average=4.000000, max=7.000000}
First Create a Double list by using Arrays.asList() method.
Conver the List to Stream using method - this returns the stream of objects. Applies this stream of data to reduction operator collect() method which takes summarizing collectors method -Collectors.summarizingDouble(), This calculates and return data encapsulated in DoubleSummaryStatistics object

DoubleSummaryStatistics methods 

The below are inbuilt methods supported by java.
Methods Description
accept(double) Add the doulbe value for summary operations. returns nothing.
Combine(DoubleSummaryStatistics) Combine other summary statics object in this. returns nothing
getAverage() return average or medan values of values, returns zero if no value exists
getCount() Returns the count of values added
getMax() returns maximum value from a list of double values. if NaN value is added, returns NaN, if no values added, returns Double.NEGATIVE_INFINITY value
getMin() returns minimum value from a double list.if NaN value is added, returns NaN, if no values added, returns Double.POSITIVE_INFINITY value
getSum() returns of sum of all double values in list. and returns zero if no values added

Related article