Java8 - java.util.IntSummaryStatistics class example

In my previous post, we discussed Introduction to Summary Statistics  in java8.
In this blog post, we are going to cover the IntSummaryStatistics class and its method with examples.

IntSummaryStatistics class 

Java8 - java.util.IntSummaryStatistics class example

IntSummaryStatistics is class for getting statistical data using like 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 class accept method example

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

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

 IntSummaryStatistics statsPrimitive = new IntSummaryStatistics();  

output is

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

IntSummaryStatistics List Stream Example 

First Created List of Employees objects using List.of() method. of() method is factory static method introduced in java9 language 

Created stream for a list of an object using method

For each stream object, map this 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 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. Syntax

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

 List<Integer> list = Arrays.asList(7,8,1,5,6);  
  IntSummaryStatistics summaryStats =;  

Output is

IntSummaryStatistics{count=5, sum=27, min=1, average=5.400000, max=8} 
  • Created a List of integers from arrays using 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

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




record the integer value for summary operations. returns nothing.


Combine other summary statics in this. returns nothing


return mean operation on list values and returns zero if no value exists


Returns the count of values recorded


returns maximum value from a list of integer values. if NaN value is added, returns NaN, if no values added, returns MAX_VALUE value


returns minimum value from a integer list.if NaN value is added, returns NaN, if no values added, returns MIN_VALUE value


returns of sum of all integer values in list. and returns zero if no values added

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.