Java Collectors summarizingDouble() Method

The summarizingDouble() method in Java, part of the java.util.stream.Collectors class, is used to collect statistical summary information about the 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. summarizingDouble() Method Syntax
  3. Understanding summarizingDouble()
  4. Examples
    • Basic Usage
    • Using summarizingDouble() with Custom Objects
  5. Real-World Use Case
  6. Conclusion

Introduction

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

summarizingDouble() Method Syntax

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

public static <T> Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)

Parameters:

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

Returns:

  • A Collector that produces a DoubleSummaryStatistics describing the statistics of the input elements.

Throws:

  • This method does not throw any exceptions.

Understanding summarizingDouble()

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

Examples

Basic Usage

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

Example

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

public class SummarizingDoubleExample {
    public static void main(String[] args) {
        List<Double> numbers = Arrays.asList(1.0, 2.5, 3.8, 4.2, 5.0);

        // Compute summary statistics for the list of numbers
        DoubleSummaryStatistics stats = numbers.stream()
                                               .collect(Collectors.summarizingDouble(Double::doubleValue));

        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: 16.5
Min: 1.0
Average: 3.3
Max: 5.0

Using summarizingDouble() with Custom Objects

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

Example

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

public class SummarizingDoubleCustomObjectExample {
    static class Product {
        String name;
        double price;

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

        double getPrice() {
            return price;
        }
    }

    public static void main(String[] args) {
        List<Product> products = Arrays.asList(
            new Product("Product A", 10.0),
            new Product("Product B", 20.5),
            new Product("Product C", 15.8),
            new Product("Product D", 30.2),
            new Product("Product E", 25.0)
        );

        // Compute summary statistics for the prices of the products
        DoubleSummaryStatistics priceStats = products.stream()
                                                     .collect(Collectors.summarizingDouble(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: 101.5
Min: 10.0
Average: 20.3
Max: 30.2

Real-World Use Case

Calculating Summary Statistics for Employee Salaries

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

Example

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

public class EmployeeSalaryStatsExample {
    static class Employee {
        String name;
        double salary;

        Employee(String name, double salary) {
            this.name = name;
            this.salary = salary;
        }

        double getSalary() {
            return salary;
        }
    }

    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
            new Employee("Alice", 50000),
            new Employee("Bob", 60000),
            new Employee("Charlie", 55000),
            new Employee("David", 75000),
            new Employee("Eve", 70000)
        );

        // Compute summary statistics for the salaries of the employees
        DoubleSummaryStatistics salaryStats = employees.stream()
                                                       .collect(Collectors.summarizingDouble(Employee::getSalary));

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

Output:

Count: 5
Sum: 310000.0
Min: 50000.0
Average: 62000.0
Max: 75000.0

Conclusion

The Collectors.summarizingDouble() method is used to collect statistical summary information about the 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