Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
integer
communitycreator

What is IntSummaryStatistics in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

For integer values, IntSummaryStatistics is used to collect statistics such as count, min, max, total, and average. This class is intended to operate with streams, although it is not required. It was introduced in Java 8.

IntSummaryStatistics is defined in the util package in Java, so you must import the util package before using the IntSummaryStatistics class, as shown below:

import java.util.IntSummaryStatistics;

The statistics offered by this class are as follows:

  1. Count of all elements
  2. Cumulative sum of the elements
  3. Minimum element
  4. Maximum element
  5. Average of all the elements

Methods

Method name Description
accept(int value) Another integer value is added to the summary information.
combine(IntSummaryStatistics other) The state of another IntSummaryStatistics object is merged using this method.
getCount() Returns the total number of values that have been seen so far.
getSum() Returns the sum of all values observed so far, or zero if none have been seen.
getMin() Returns the smallest recorded integer value.
getMax() Returns the largest recorded integer value.
getAverage() Returns the arithmetic mean of all values seen so far, or zero if none have been seen.

Examples

In the code below, we are computing the statistics from a list of integer values. The accept method is used to take the int value into the summary information.

import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
public class Main {
public static void main(String args[])
{
List<Integer> integerList = new ArrayList<>();
integerList.add(154);
integerList.add(65);
integerList.add(456);
integerList.add(765);
integerList.add(23456);
integerList.add(34565432);
IntSummaryStatistics intSummaryStatistics = new IntSummaryStatistics();
for(int d: integerList) intSummaryStatistics.accept(d);
System.out.println("Count of the elements - " + intSummaryStatistics.getCount());
System.out.println("Sum of the elements - " + intSummaryStatistics.getSum());
System.out.println("Minimum element - " + intSummaryStatistics.getMin());
System.out.println("Maximum element - " + intSummaryStatistics.getMax());
System.out.println("Average of the elements - " + intSummaryStatistics.getAverage());
}
}

In the code below, we use streams to get summary statistics. The collect() method and Collectors.summarizingInt is used to collect the statistics.

import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.stream.Collectors;
import java.util.List;
public class Main {
public static void main(String args[])
{
List<Integer> integerList = new ArrayList<>();
integerList.add(154);
integerList.add(65);
integerList.add(456);
integerList.add(765);
integerList.add(23456);
integerList.add(34565432);
IntSummaryStatistics intSummaryStatistics = integerList.stream().collect(Collectors.summarizingInt(e -> e));
System.out.println("Count of the elements - " + intSummaryStatistics.getCount());
System.out.println("Sum of the elements - " + intSummaryStatistics.getSum());
System.out.println("Minimum element - " + intSummaryStatistics.getMin());
System.out.println("Maximum element - " + intSummaryStatistics.getMax());
System.out.println("Average of the elements - " + intSummaryStatistics.getAverage());
}
}

RELATED TAGS

java
integer
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring