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:
-
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()
- URL class in Java with Examples
- StringBuilder Class in Java with Examples
- AbstractSet Class in Java with Examples
- Java AWT | Cursor class with examples
- Calendar Class in Java with examples
- JavaFX | Cursor class with examples
- Date class in Java (With Examples)
- Java.lang.InheritableThreadLocal Class with Examples
- ResourceBundle and ListResourceBundle class in Java with Examples
- BitSet class methods in Java with Examples | Set 3
- Java.util.LongSummaryStatistics class with Examples
- Java.util.IntSummaryStatistics class with Examples
- Math class methods in Java with Examples | Set 2
- Java Guava | pow(long b, int k) of LongMath Class with Examples
- util.date class methods in Java with Examples
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
Recommended Posts:
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.



