Java.util.LongSummaryStatistics class with Examples

The java.util package contains the LongSummaryStatistics class. When dealing with a stream of long integers, it accepts a collection of Long objects and can be advantageous. It keeps track of how many values it has processed, how much they have added up to, and other statistics. The class is compatible with Streams as well. Since it keeps track of the running total, average, etc., of the numbers, it is essential for manipulating data from statistical analyses.

The Class Hierarchy is given by

The Constructor that is used is given by:

LongSummaryStatistics(): A default constructor sets the count and total to zero by default and sets the max to Long.MIN_VALUE and min to Long.MAX_VALUE.

Syntax:

The Methods that are implemented by this function is given by:

1. accept(): The function adds the provided integer or long value to the statistical data because it is overloaded.

Syntax:

public void accept(long value)

2. combine(): The given LongSummaryStatistics object's statistical data and the current statistical data are combined by the function.

Syntax:

3. getCount(): Returns back the total number of long values that were handled.

Syntax:

4. getSum(): Returns back the total of all the long values it has processed.

Syntax:

5. getAverage(): Returns back the average of all the long values it has processed.

Syntax:

6. getMin() : Returns back the minimum value of all the long values it has processed.

Syntax:

7. getMax(): Returns back the maximum value of all the long values it has processed.

Syntax:

8. toString(): Returns the string representation of each element of statistical data that is present in the object.

Syntax:

Example:

The given Java program uses the LongSummaryStatistics class to compute and display summary statistics for a collection of long values. It starts by generating a linked list of Long values and an instance of LongSummaryStatistics. The application increases the list by ten long values (10 to 100). Each value is given into the LongSummaryStatistics object using the accept method once an iterator has traversed the list. Following the processing of all the data, the program outputs a number of statistics, such as the count, average, sum, maximum, and minimum of the data. The LongSummaryStatistics object's string representation, which contains all of the calculated statistics, is printed at the end.

Implementation:

FileName: LongSummaryExample.java

Output:

The count of all the given values is: 10
The average of all the given values is: 55.0
The sum of all the given values is: 550
The maximum of all the given values is: 100
The minimum of all the given values is: 10
The string representation of the given values is: 
LongSummaryStatistics{count=10, sum=550, min=10, average=55.000000, max=100}