Java.util.DoubleSummaryStatistics class with Examples

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()
    
  9. Example To demonstrate DoubleSummaryStatistics in action.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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());
        }
    }

    chevron_right

    
    

    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



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.