Java DoubleStream reduce() Method

The reduce() method in Java, part of the java.util.stream.DoubleStream interface, is used to perform a reduction on the elements of a stream using an associative accumulation function and return an OptionalDouble. This method is useful when you need to aggregate elements of a stream to a single result.

Table of Contents

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

Introduction

The reduce() method is a terminal operation that performs a reduction on the elements of the stream using an associative accumulation function. It returns an OptionalDouble describing the reduced value, if any.

reduce() Method Syntax

There are three overloaded versions of the reduce() method in DoubleStream:

  1. Single accumulator function:
OptionalDouble reduce(DoubleBinaryOperator op)
  1. Initial value and accumulator function:
double reduce(double identity, DoubleBinaryOperator op)
  1. Initial value, accumulator function, and combiner function:
double reduce(double identity, DoubleBinaryOperator accumulator, DoubleBinaryOperator combiner)

Parameters:

  • op or accumulator: A DoubleBinaryOperator that represents the reduction operation.
  • identity: The initial value for the reduction operation.
  • combiner: A DoubleBinaryOperator used to combine the results of the accumulator functions.

Returns:

  • An OptionalDouble describing the reduced value, or the reduced value itself if an initial value is provided.

Throws:

  • This method does not throw any exceptions.

Understanding reduce()

The reduce() method allows you to combine the elements of a DoubleStream into a single value using a specified accumulation function. If no initial value is provided, the result is wrapped in an OptionalDouble to handle the case where the stream might be empty.

Examples

Basic Usage

To demonstrate the basic usage of reduce(), we will create a DoubleStream and use reduce() to calculate the sum of its elements.

Example

import java.util.OptionalDouble;
import java.util.stream.DoubleStream;

public class ReduceExample {
    public static void main(String[] args) {
        DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);

        // Reduce the stream to calculate the sum of its elements
        OptionalDouble sum = doubleStream.reduce((a, b) -> a + b);

        // Print the sum if present
        sum.ifPresent(System.out::println);
    }
}

Output:

16.5

Using reduce() with Initial Value

This example shows how to use reduce() with an initial value to calculate the product of the elements in a DoubleStream.

Example

import java.util.stream.DoubleStream;

public class ReduceWithInitialValueExample {
    public static void main(String[] args) {
        DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);

        // Reduce the stream to calculate the product of its elements with an initial value of 1
        double product = doubleStream.reduce(1.0, (a, b) -> a * b);

        // Print the product
        System.out.println(product);
    }
}

Output:

193.26120000000003

Real-World Use Case

Calculating the Average Temperature

In real-world applications, the reduce() method can be used to calculate the average temperature from a stream of temperature readings.

Example

import java.util.stream.DoubleStream;

public class AverageTemperatureExample {
    public static void main(String[] args) {
        DoubleStream temperatureReadings = DoubleStream.of(25.3, 26.7, 24.8, 27.5, 30.1);

        // Calculate the sum of the temperature readings
        double sum = temperatureReadings.reduce(0.0, (a, b) -> a + b);

        // Calculate the average temperature
        double averageTemperature = sum / 5;

        // Print the average temperature
        System.out.println("Average temperature: " + averageTemperature);
    }
}

Output:

Average temperature: 26.880000000000003

Conclusion

The DoubleStream.reduce() method is used to perform a reduction on the elements of a stream using an associative accumulation function. This method is particularly useful for aggregating elements of a stream into a single result. By understanding and using this method, you can efficiently manage and process streams of double values in your Java applications.

Comments