The LongSummaryStatistics class is present in java.util package. It takes a collection of Long objects and is useful in the circumstances when we are dealing with a stream of large integers. 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 values and hence can be used in the manipulation of statistical data.
Class Hierarchy
java.lang.Object ↳ java.util.LongSummaryStatistics
Constructors
- LongSummaryStatistics(): A default constructor which initializes the count and sum to zero, and sets max to Long.MIN_VALUE and min to Long.MAX_VALUE.
public LongSummaryStatistics()
Methods:
- accept(): The function is overloaded and it adds the passed integer or long value into the statistical data.
public void accept(int value) public void accept(long value)
- combine(): The function combines the statistical data of the passed LongSummaryStatistics object with the current statistical data.
public void combine(LongSummaryStatistics other)
- getCount(): Returns the count of the number of long values processed.
public final long getCount()
- getSum(): Returns the sum of all the long values processed.
public final long getSum()
- getAverage(): Returns the average of all the long values processed.
public final double getAverage()
- getMin() : Returns the minimum integer of all the long values processed.
public final long getMin()
- getMax(): Returns the maximum integer of all the long values processed.
public final long getMax()
- toString(): Returns the string representation of all the statistical data contained in the object.
public String toString()
Example to demonstrate LongSummaryStatistics in action.
// Java program to demonstrate // LongSummaryStatistics class import java.util.*; public class LongSummaryStatisticsDemo { public static void main(String[] args) { LongSummaryStatistics longSummaryStatistics = new LongSummaryStatistics(); List<Long> list = new LinkedList<>(); list.add(100l); list.add(200l); list.add(300l); list.add(400l); list.add(500l); list.add(600l); list.add(700l); list.add(800l); list.add(900l); list.add(1000l); Iterator<Long> iterator = list.listIterator(); while (iterator.hasNext()) { // Add the values to the LongSummaryStatistics object longSummaryStatistics.accept(iterator.next()); } // Use various methods to obtain the data System.out.println( "The count of values is " + longSummaryStatistics.getCount()); System.out.println( "The average of values is " + longSummaryStatistics.getAverage()); System.out.println( "The sum of values is " + longSummaryStatistics.getSum()); System.out.println( "The maximum of values is " + longSummaryStatistics.getMax()); System.out.println( "The minimum of values is " + longSummaryStatistics.getMin()); System.out.println( "The string representation is " ); System.out.println(longSummaryStatistics.toString()); } } |
The count of values is 10 The average of values is 550.0 The sum of values is 5500 The maximum of values is 1000 The minimum of values is 100 The string representation is LongSummaryStatistics{count=10, sum=5500, min=100, average=550.000000, max=1000}
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/LongSummaryStatistics.html