{

java8 - java.util.DoubleSummaryStatistics class example


Java8 java.time.DoubleSummaryStatistics  Class example

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

What is DoubleSummaryStatistics in java?

DoubleSummaryStatistics is one of the classes for creating state objects 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 these 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.

import java.util.DoubleSummaryStatistics;

public class DoubleSummaryStatisticsExample {
    public static void main(String[] args) {
        DoubleSummaryStatistics statsPrimitive = new DoubleSummaryStatistics();
        statsPrimitive.accept(10.0);
        statsPrimitive.accept(50.0);
        statsPrimitive.accept(2.0);
        statsPrimitive.accept(3.0);
        statsPrimitive.accept(20.0);
        System.out.println(statsPrimitive);
    }
}

The output of the above code

DoubleSummaryStatistics{count=5, sum=85.000000, min=2.000000, average=17.000000, max=50.000000} 

How to create a DoubleSummaryStatistics Object example

This is one of the ways to create an unmodified collection for limited data set and returns a list.

  • First, Create a List using factor static method of() which was introduced in java8.
  • Please see List.of() method
  • Create DoubleSummaryStatistics object using a stream from List of Objects 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 = empLists.stream()
                .mapToDouble((value) -> value.getSalary()).summaryStatistics();
        System.out.println(sumaryStats);
    }
}
class Employee {

    double salary;

    public Employee(double salary) {
        super();
        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 the DoubleStream summaryStatistics() method.

import java.util.DoubleSummaryStatistics;
import java.util.stream.DoubleStream;

public class DoubleSummaryStatisticsExample {
    public static void main(String[] args) {
        DoubleStream doubleStream = DoubleStream.of(5.0, 4.0, 1.0, 7.0, 3.0);
        DoubleSummaryStatistics summaryStats = doubleStream.summaryStatistics();
        System.out.println(summaryStats);
    }
}

Output:

DoubleSummaryStatistics{count=5, sum=20.000000, min=1.000000, average=4.000000, max=7.000000}

Following are a sequence of steps

  • 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 a double version Stream class in java8.
  • Calling summaryStatistics() method to get DoubleSummaryStatistics object which has summary data like count,sum,min,average and max of double data.
  • This is a reduction operation of Stream API and takes an input array of values and gives an output of summary of data.

Collectors summarizingDouble() method example

Collectors.summarizingDouble() is a static method defined in java.util.stream 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 java.util.stream.Collectors which convert each element to Double by mapping each element to Function and returns DoubleSummaryStatistics class.

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

public class DoubleSummaryStatisticsExample {
    public static void main(String[] args) {
        List<Double> list = Arrays.asList(5.0, 4.0, 1.0, 7.0, 3.0);
        DoubleSummaryStatistics summaryStats1 = list.stream().collect(Collectors.summarizingDouble(Double::doubleValue));
        System.out.println(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.
  • Convert the List to Stream using list.stream() method, It 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 List

The below are inbuilt methods supported by java.

Methods Description
accept(double) Add the double value for summary operations. returns nothing.
Combine(DoubleSummaryStatistics) Combine other summary statics object in this. returns nothing
getAverage() return average or median 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

Conclusion

Learned DoubleSummaryStatistics object creation and methods usage with example

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.