Open In App

IntStream summaryStatistics() Method in Java

Last Updated : 07 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the IntStream summaryStatistics() method returns an IntSummaryStatistics object that provides various summary statistics about the elements in the IntStream. This includes the count, average, minimum, maximum, and sum of the elements. This method is a terminal operation, i.e., it may traverse the stream to produce a result or a side-effect.

In this article, we are going to discuss how IntStream.summaryStatistics() simplifies the process of computing statistical measures in Java streams.

Declaration IntStream summaryStatistics()

The declaration is:

IntSummaryStatistics summaryStatistics()

  • Parameter: This method does not take any parameters.
  • Return Type: This method returns an IntSummaryStatistics describing various summary data about the elements of this stream.

What is IntSummaryStatistics?

It is a class in java.util package that collects statistics such as:

  • Count: Total number of elements
  • Sum: The sum of all the elements
  • Min: The smallest value.
  • Max: The largest value.
  • Average: The arithmetic mean.

Why Use summaryStatistics()?

IntStream summaryStatistics() is a special case of a reduction. A reduction, also known as a fold, combines the elements of a stream using a combining function such as summing values, finding the maximum, etc. summaryStatistics() offers a concise way to gather all of them at once, instead of using separate operations for each metric.

Now, we are going to discuss some examples for better understanding.


Examples of Java IntStream summaryStatistics() Method

Example 1: Using IntStream summaryStatistics() to get the IntSummaryStatistics of elements present in given IntStream.

Java
// Java program for IntStream summaryStatistics()
import java.util.stream.IntStream;
import java.util.IntSummaryStatistics; 

class Geeks {

    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream s = IntStream.of(4, 5, 6, 7);

        // Using IntStream summaryStatistics()
        IntSummaryStatistics sd = 
                        s.summaryStatistics();

        // Displaying the various summary data
        // about the elements of the stream
        System.out.println(sd);
    }
}

Output
IntSummaryStatistics{count=4, sum=22, min=4, average=5.500000, max=7}

Explanation: Here, in this example, we are using IntStream.summaryStatistics() to calculate the count, sum, min, average and max for the input stream and then printing the output.

Example 2: Using IntStream summaryStatistics() to get the IntSummaryStatistics of elements in a given range.

Java
// Java program for IntStream summaryStatistics()
import java.util.stream.IntStream;
import java.util.IntSummaryStatistics;  

class Geeks {

    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream of elements in range [5, 9)
        IntStream s = IntStream.range(5, 9);

        // Using IntStream summaryStatistics()
        IntSummaryStatistics sd =
                       s.summaryStatistics();

        // Displaying the various summary data
        // about the elements of the stream
        System.out.println(sd);
    }
}

Output
IntSummaryStatistics{count=4, sum=26, min=5, average=6.500000, max=8}

Explanation: Here, in this example we are creating an IntStream with elements in the range from 5 and 8 with the help of IntStream.range(5,9) and then we are calculating count, sum, min, average and max and then printing the output.

When to Use summaryStatistics()

The summaryStatistics() method is useful in many scenarios which are listed below:

  • This method allows us to compute all the essential statistical values like count, sum, minimum, maximum, average in just a single operation.
  • This method can be used to collect information such as execution times, memory usage or other system metrics.
  • Instead of calling sum(), min(), max() separately, we can use this method to get everything together.

Advantages

The advantages of summaryStatistics() method is listed below:

  • This method get all the statistics in one traversal
  • It reduces the boilerplate code and also improves the readability.
  • It is very easy to modify and extend.

Practice Tags :

Similar Reads