IntSummaryStatistics Class in Java
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; System.out.println("Max is " + Arrays.stream(arr).max().getAsInt()); System.out.println("Min is " + Arrays.stream(arr).min().getAsInt()); System.out.println("Average is " + Arrays.stream(arr).average().getAsDouble()); System.out.println("Count is " + Arrays.stream(arr).count()); System.out.println("Sum is " + Arrays.stream(arr).sum()); } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.IntSummaryStatistics; class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; // Get statistics of an array using IntSummaryStatistics IntSummaryStatistics stats = Arrays.stream(arr).summaryStatistics(); System.out.println(stats); System.out.println("Max is " + stats.getMax()); System.out.println("Min is " + stats.getMin()); System.out.println("Average is " + stats.getAverage()); System.out.println("Count is " + stats.getCount()); System.out.println("Sum is " + stats.getSum()); } } |
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).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.IntSummaryStatistics; class Main { public static void main(String[] args) { IntSummaryStatistics stats1 = new IntSummaryStatistics(); System.out.println(stats1); IntSummaryStatistics stats2 = Arrays.stream(new int[]{}).summaryStatistics(); System.out.println(stats2); // both will print IntSummaryStatistics // {count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648} } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); // Get statistics of a list using IntSummaryStatistics IntSummaryStatistics stats = nums.stream() .mapToInt(Integer::intValue) .summaryStatistics(); /* // or use IntSummaryStatistics stats = new IntSummaryStatistics(); for (int num : nums) { stats.accept(num); } */ System.out.println(stats); System.out.println("Max is " + stats.getMax()); System.out.println("Min is " + stats.getMin()); System.out.println("Average is " + stats.getAverage()); System.out.println("Count is " + stats.getCount()); System.out.println("Sum is " + stats.getSum()); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.IntSummaryStatistics; class Main { public static void main(String[] args) { int[] arr1 = { 1, 2, 3, 4, 5 }; int[] arr2 = { 6, 7, 8, 9, 10 }; IntSummaryStatistics stats1 = Arrays.stream(arr1).summaryStatistics(); System.out.println(stats1); IntSummaryStatistics stats2 = Arrays.stream(arr2).summaryStatistics(); System.out.println(stats2); stats1.combine(stats2); System.out.println(stats1); } } |
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.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)