Java.util.IntSummaryStatistics class with Examples

The java.util package consists of the IntSummaryStatistics class. When performing operations on a stream of integers, it accepts a collection of Integer objects and can be effective. It keeps track of how many numbers it has processed, how much they have added up to, and other statistics. The class is compatible with Streams as well. It can be used to manipulate statistical data because it keeps track of the operating sum, average, etc., of the integers. Thus making it convenient.

The Class Hierarchy of the IntSummaryStatistics class is:

The Constructors that can be implemented are:

1. IntSummaryStatistics(): A default constructor sets the count and total to zero by default and sets max to Integer.MIN_VALUE and min to Integer.MAX_VALUE.

Syntax:

2. IntSummaryStatistics(count, min, max, sum): Initializes all the distinct data members using the parameters that were given in during the process of calling method.

Syntax:

The Methods that are used are:

1. accept() - The given integer is added by this function to the statistical data.

Syntax:

2. combine(): This function combines the current statistics data with the data from the given IntSummaryStatistics object.

Syntax:

3. getCount(): The count of the numbers that have been handled is returned by this method.

Syntax:

4. getSum(): The total of all the integers processed is the result of this method.

Syntax:

5. getAverage(): The average of all the integers processed is the result of this method.

Syntax:

6. getMin(): The minimum value of all the integers processed is the result of this method.

Syntax:

7. getMax(): The maximum value of all the integers processed is the result of this method.

Syntax:

8. toString(): The string representation of each element of statistical information present in the object is what this method returns.

Syntax:

Example:

The program in Java demonstrates the way to collect statistics about a list of integers using the IntSummaryStatistics class. The first step is to initialize a list of integers and create an instance of IntSummaryStatistics. Each integer is added to the IntSummaryStatistics object as iteratively traversing the list using an iterator. Count, average, sum, maximum, and minimum values of the list members are among the statistical measurements printed by the program after the statistics object has been filled. Ultimately, all of these details are included in a single string that is the string representation of the summary statistics.

Implementation:

FileName: IntSummaryStatisticsExample.java

Output:

The count of all the given values is: 10
The average of all the given values is: 49.5
The sum of all the given values is: 495
The maximum of all the given values is: 99
The minimum of all the given values is: 0
The string representation of the given values is: 
IntSummaryStatistics{count=10, sum=495, min=0, average=49.500000, max=99}