Java8 - java.util.IntSummaryStatistics class example

In my previous post, we discussed Introduction to Summary Statistics in java8. It covers the IntSummaryStatistics class and its method with examples.

What is IntSummaryStatistics class in java?

IntSummaryStatistics is a class for getting statistical data using sum, max, min, average, and count operations on integer data.

IntSummaryStatistics is for Integer datatype defined in java.util package.

There are other classes like DoubleSummaryStatistics for double data type and LongSummaryStatistics for the long datatype.

The object created can be designed to work with java8 streams and lambda expressions.

There are many ways we can create IntSummaryStatistics objects.

IntSummaryStatistics accept method example

using IntSummaryStatistics.accept() method which takes integer value as input and record this value to object.

It is one way of calculating statistical operations on integer data and it is simple.

import java.util.IntSummaryStatistics;
public class IntSummaryStatisticsExample {
    public static void main(String[] args) {
        IntSummaryStatistics statsPrimitive = new IntSummaryStatistics();


IntSummaryStatistics{count=6, sum=42, min=2, average=7.000000, max=18}

IntSummaryStatistics List Stream lambda expressionExample

  • First Created a List of Employees objects using the List.of() method. of() method is factory static method introduced in java8 language.
  • Created stream for a list of an object using method.
  • For each stream object, map ToIntFunction using mapToInt method.
  • ToIntFunction is a functional interface which takes an object as input and output integer value. Please see ToIntFunction examples.
  • mapToInt() returns IntStream using lambda expression which finally called summaryStatistics reducer operation to return IntSummaryStatistics object. This object holds statistics data encapsulated in this object.
import java.util.IntSummaryStatistics;
import java.util.List;

public class Summary2 {

    public static void main(String[] args) {
        List<Employee> empLists = List.of(new Employee(5000), new Employee(2000), new Employee(21000),
                new Employee(2500));
        IntSummaryStatistics sumaryStats = -> value.getSalary()).summaryStatistics();



class Employee {

    Integer salary;

    public Employee(Integer salary) {
        this.salary = salary;

    public Integer getSalary() {
        return salary;

The output of the above code execution is

IntSummaryStatistics{count=4, sum=30500, min=2000, average=7625.000000, max=21000}

Collectors summarizingInt() method example

Collectors.summarizingInt() is a static method defined in package.


public static <T> Collector<T,?,IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper)

This takes function,on which this function accepts objects and produces integer result as output. ToIntFunction is a functional interface provided in java.util.function package

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;

public class summaryStats {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(7,8,1,5,6);
        IntSummaryStatistics summaryStats =;


IntSummaryStatistics{count=5, sum=27, min=1, average=5.400000, max=8}
  • Created a List of integers from arrays using the Arrays.asList() method
  • Created stream object using stream() which returns the stream of objects
  • Finally, Call collect to reduce operation using the collect method with input Collectors.summarizingInt
  • returns the calculated encapsulated in IntSummaryStatistics

IntStream summaryStatistics() method example

IntStream is an integer version of Stream API. IntStream summaryStatistics() provides to get Integer Summary Statics object.

import java.util.IntSummaryStatistics;

public class summaryStats {
    public static void main(String[] args) {
        IntStream intStream = IntStream.of(8,6,4,8,3);
        IntSummaryStatistics summaryStats = intStream.summaryStatistics();

The output of the above code is

IntSummaryStatistics{count=5, sum=29, min=3, average=5.800000, max=8}

Created IntStream from an array of numbers IntStream.of() method Returned the summary statistics data by calling IntStream.summaryStatistics method.

IntSummaryStatistics methods

The below are inbuilt methods supported by java for summary statistics operations on integer data

accept(integer)record the integer value for summary operations. returns nothing.
Combine(IntSummaryStatistics)Combine other summary statics in this. returns nothing
getAverage()return mean operation on list values and returns zero if no value exists
getCount()Returns the count of values recorded
getMax()returns maximum value from a list of integer values. if NaN value is added, returns NaN, if no values added, returns MAX_VALUE value
getMin()returns minimum value from a integer list.if NaN value is added, returns NaN, if no values added, returns MIN_VALUE value
getSum()returns of sum of all integer values in list. and returns zero if no values added


Learned IntSummaryStatistics object creation and methods usage with example