Skip to content
Related Articles

Related Articles

Improve Article

Java.util.DoubleSummaryStatistics class with Examples

  • Last Updated : 27 Jun, 2019

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:

  1. 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:

  1. accept(): nThis function adds the passed double into the statistical data.

    Syntax:

    public void accept(double value)
    
  2. combine(): This function combines the statistical data of the passed DoubleSummaryStatistics object with the current statistical data.

    Syntax:

    public void combine(DoubleSummaryStatistics other)
    
  3. getCount(): This method returns the count of the number of doubles processed.

    Syntax:



    public final long getCount()
    
  4. getSum(): This method returns the sum of all the doubles processed.

    Syntax:

    public final long getSum()
    
  5. getAverage(): This method returns the average of all the doubles processed.

    Syntax:

    public final double getAverage()
    
  6. getMin(): This method returns the minimum double of all the doubles processed.

    Syntax:

    public final double getMin()
    
  7. getMax(): This method returns the maximum double of all the doubles processed.

    Syntax:

    public final double getMax()
    
  8. 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());
        }
    }
    Output:
    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

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




    My Personal Notes arrow_drop_up
Recommended Articles
Page :