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();
        statsPrimitive.accept(7);
        statsPrimitive.accept(6);
        statsPrimitive.accept(4);
        statsPrimitive.accept(2);
        statsPrimitive.accept(18);
        statsPrimitive.accept(5);
        System.out.println(statsPrimitive);
    }
}

output:

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 List.stream() 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 = empLists.stream().mapToInt((value) -> value.getSalary()).summaryStatistics();
        System.out.println(sumaryStats);

    }

}

class Employee {

    Integer salary;

    public Employee(Integer salary) {
        super();
        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 java.util.stream 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

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

public class summaryStats {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(7,8,1,5,6);
        IntSummaryStatistics summaryStats = list.stream().collect(Collectors.summarizingInt(Integer::intValue));
        System.out.println(summaryStats);
    }
}

Output:

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;
import java.util.stream.IntStream;

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

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

MethodsDescription
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

Conclusion

Learned IntSummaryStatistics object creation and methods usage with example