This post will introduce IntSummaryStatistics class in Java, which collects statistics such as count of elements, the minimum and maximum sum, and the average of elements in an IntStream.

The IntSummaryStatistics class is a useful class in Java that can help we collect and analyze statistics on a stream of integers. It can keep track of the count, sum, min, max, and average of the values in the stream.

1. Getting statistics of an array

First, let’s see the naive way of getting statistics such as count, min, max, sum, and the average of elements in an IntStream. It involves getting the stream every time and converting Optional to its corresponding primitive value for min, max, and average operations.

Download  Run Code

Output:

Max is 5
Min is 1
Average is 3.0
Count is 5
Sum is 15

 
A better solution is to get an instance of IntSummaryStatistics, which gives a state object for collecting statistics such as count, min, max, sum, and average. We can create instance of the IntSummaryStatistics class from the stream elements by calling the summaryStatistics() method. Then we can access the statistics by using the corresponding methods of the IntSummaryStatistics class, such as getMax(), getMin(), getAverage(), getCount(), and getSum(). For example:

Download  Run Code

Output:

IntSummaryStatistics{count=5, sum=15, min=1, average=3.000000, max=5}
Max is 5
Min is 1
Average is 3.0
Count is 5
Sum is 15

 
If an IntSummaryStatistics object is created from an empty stream or using the default constructor, it has the following initial values: getMax() returns the smallest possible integer (Integer.MIN_VALUE), getMin() returns the largest possible integer (Integer.MAX_VALUE), getAverage() returns zero (0.0), and both getCount() and getSum() also return zero (0).

Download  Run Code

2. Getting statistics of a list

To create an IntSummaryStatistics object from a List of Integers, we need to get the IntStream and call the summaryStatistics() method on the stream. For example:

Download  Run Code

Output:

IntSummaryStatistics{count=5, sum=15, min=1, average=3.000000, max=5}
Max is 5
Min is 1
Average is 3.0
Count is 5
Sum is 15

3. Combining two IntSummaryStatistics objects

We can also combine two IntSummaryStatistics objects to get the combined statistics of both streams by using the combine() method of the IntSummaryStatistics class. This method takes another IntSummaryStatistics object as a parameter and merges its state with the current object. For example, if we have two IntSummaryStatistics objects named stats1 and stats2, we can combine them by calling stats1.combine(stats2). This will update the count, sum, min, max, and average of stats1 with the values from stats2.

Download  Run Code

Output:

IntSummaryStatistics{count=5, sum=15, min=1, average=3.000000, max=5}
IntSummaryStatistics{count=5, sum=40, min=6, average=8.000000, max=10}
IntSummaryStatistics{count=10, sum=55, min=1, average=5.500000, max=10}

That’s all about the IntSummaryStatistics class in Java to collect IntStream statistics.