The DoubleSummaryStatistics class is present in java.util package. It takes a collection of Double objects and is useful in the circumstances when we are dealing with a stream of large precision real numbers. It maintains a count of the number of values it has processed, their sum and various other statistics. The class can also be used with Streams.
It is useful in the sense that it maintains a running sum, average, etc. of the doubles and hence can be used in the manipulation of statistical data.
Class Hierarchy
java.lang.Object ↳ java.util.DoubleSummaryStatistics
Constructors:
- DoubleSummaryStatistics(): A default constructor which initializes the count and sum to zero, and sets max to Double.NEGATIVE_INFINITY and min to Double.POSITIVE_INFINITY.
Syntax:
public DoubleSummaryStatistics()
Methods:
- accept(): nThis function adds the passed double into the statistical data.
Syntax:
public void accept(double value)
- combine(): This function combines the statistical data of the passed DoubleSummaryStatistics object with the current statistical data.
Syntax:
public void combine(DoubleSummaryStatistics other)
- getCount(): This method returns the count of the number of doubles processed.
Syntax:
public final long getCount()
- getSum(): This method returns the sum of all the doubles processed.
Syntax:
public final long getSum()
- getAverage(): This method returns the average of all the doubles processed.
Syntax:
public final double getAverage()
- getMin(): This method returns the minimum double of all the doubles processed.
Syntax:
public final double getMin()
- getMax(): This method returns the maximum double of all the doubles processed.
Syntax:
public final double getMax()
- toString(): This method returns the string representation of all the statistical data contained in the object.
Syntax:
public String toString()
Example To demonstrate DoubleSummaryStatistics in action.
// Java program to demonstrate // DoubleSummaryStatistics class import java.util.*; public class DoubleSummaryStatisticsDemo { public static void main(String[] args) { DoubleSummaryStatistics doubleSummaryStatistics = new DoubleSummaryStatistics(); List<Double> list = new LinkedList<>(); list.add( 95.7 ); list.add( 234.6767 ); list.add( 243.5 ); list.add( 50.0 ); list.add( 45.6 ); list.add( 45664.0 ); list.add( 7007.777 ); list.add( 5677.0 ); list.add( 0.0 ); list.add( 45664.7 ); Iterator<Double> iterator = list.listIterator(); while (iterator.hasNext()) { // Add the doubles to the // DoubleSummaryStatistics object doubleSummaryStatistics .accept(iterator.next()); } // Use various methods to obtain the data System.out.println( "The count of values is " + doubleSummaryStatistics .getCount()); System.out.println( "The average of values is " + doubleSummaryStatistics .getAverage()); System.out.println( "The sum of values is " + doubleSummaryStatistics .getSum()); System.out.println( "The maximum of values is " + doubleSummaryStatistics .getMax()); System.out.println( "The minimum of values is " + doubleSummaryStatistics .getMin()); System.out.println( "The string representation is" ); System.out.println(doubleSummaryStatistics .toString()); } } |
The count of values is 10 The average of values is 10468.29537 The sum of values is 104682.9537 The maximum of values is 45664.7 The minimum of values is 0.0 The string representation is DoubleSummaryStatistics{count=10, sum=104682.953700, min=0.000000, average=10468.295370, max=45664.700000}
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/DoubleSummaryStatistics.html