Java Collectors summarizingInt() Method

The summarzingInt() method in Java, part of the java.util.stream.Collectors class, is used to collect statistical summary information about the integer elements of a stream. This method is useful when you need to compute summary statistics such as count, sum, min, average, and max for a collection of elements.

Table of Contents

  1. Introduction
  2. summarizingInt() Method Syntax
  3. Understanding summarizingInt()
  4. Examples
    • Basic Usage
    • Using summarizingInt() with Custom Objects
  5. Real-World Use Case
  6. Conclusion

Introduction

The summarizingInt() method returns a Collector that accumulates the count, sum, min, average, and max of an integer-valued function applied to the input elements. The result is an instance of IntSummaryStatistics.

summarizingInt() Method Syntax

The syntax for the summarizingInt() method is as follows:

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

Parameters:

  • mapper: A function that extracts an integer-valued property from an element.

Returns:

  • A Collector that produces an IntSummaryStatistics describing the statistics of the input elements.

Throws:

  • This method does not throw any exceptions.

Understanding summarizingInt()

The summarizingInt() method allows you to compute summary statistics for elements of a stream. The resulting IntSummaryStatistics object provides methods to retrieve the count, sum, min, average, and max values.

Examples

Basic Usage

To demonstrate the basic usage of summarizingInt(), we will create a list of integers and compute summary statistics for the elements.

Example

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

public class SummarizingIntExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        // Compute summary statistics for the list of numbers
        IntSummaryStatistics stats = numbers.stream()
                                            .collect(Collectors.summarizingInt(Integer::intValue));

        System.out.println("Count: " + stats.getCount());
        System.out.println("Sum: " + stats.getSum());
        System.out.println("Min: " + stats.getMin());
        System.out.println("Average: " + stats.getAverage());
        System.out.println("Max: " + stats.getMax());
    }
}

Output:

Count: 5
Sum: 15
Min: 1
Average: 3.0
Max: 5

Using summarizingInt() with Custom Objects

This example shows how to use summarizingInt() with a stream of custom objects to compute summary statistics for a specific property.

Example

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

public class SummarizingIntCustomObjectExample {
    static class Product {
        String name;
        int price;

        Product(String name, int price) {
            this.name = name;
            this.price = price;
        }

        int getPrice() {
            return price;
        }
    }

    public static void main(String[] args) {
        List<Product> products = Arrays.asList(
            new Product("Product A", 10),
            new Product("Product B", 20),
            new Product("Product C", 15),
            new Product("Product D", 30),
            new Product("Product E", 25)
        );

        // Compute summary statistics for the prices of the products
        IntSummaryStatistics priceStats = products.stream()
                                                  .collect(Collectors.summarizingInt(Product::getPrice));

        System.out.println("Count: " + priceStats.getCount());
        System.out.println("Sum: " + priceStats.getSum());
        System.out.println("Min: " + priceStats.getMin());
        System.out.println("Average: " + priceStats.getAverage());
        System.out.println("Max: " + priceStats.getMax());
    }
}

Output:

Count: 5
Sum: 100
Min: 10
Average: 20.0
Max: 30

Real-World Use Case

Calculating Summary Statistics for Employee Ages

In real-world applications, the summarizingInt() method can be used to compute summary statistics for numeric properties such as employee ages.

Example

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

public class EmployeeAgeStatsExample {
    static class Employee {
        String name;
        int age;

        Employee(String name, int age) {
            this.name = name;
            this.age = age;
        }

        int getAge() {
            return age;
        }
    }

    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
            new Employee("Alice", 30),
            new Employee("Bob", 25),
            new Employee("Charlie", 35),
            new Employee("David", 40),
            new Employee("Eve", 45)
        );

        // Compute summary statistics for the ages of the employees
        IntSummaryStatistics ageStats = employees.stream()
                                                 .collect(Collectors.summarizingInt(Employee::getAge));

        System.out.println("Count: " + ageStats.getCount());
        System.out.println("Sum: " + ageStats.getSum());
        System.out.println("Min: " + ageStats.getMin());
        System.out.println("Average: " + ageStats.getAverage());
        System.out.println("Max: " + ageStats.getMax());
    }
}

Output:

Count: 5
Sum: 175
Min: 25
Average: 35.0
Max: 45

Conclusion

The Collectors.summarizingInt() method is used to collect statistical summary information about the integer elements of a stream. This method is particularly useful for computing summary statistics such as count, sum, min, average, and max. By understanding and using this method, you can efficiently manage statistical operations in your Java applications.

Comments