Java DoubleStream average() Method

The average() method in Java, part of the java.util.stream.DoubleStream interface, is used to calculate the average of the elements in a stream. This method is useful when you need to compute the average value of a sequence of double values.

Table of Contents

  1. Introduction
  2. average() Method Syntax
  3. Understanding average()
  4. Examples
    • Basic Usage
    • Handling an Empty Stream
  5. Real-World Use Case
  6. Conclusion

Introduction

The average() method returns an OptionalDouble describing the arithmetic mean of the elements of the stream, or an empty OptionalDouble if the stream is empty. This is a terminal operation.

average() Method Syntax

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

OptionalDouble average()

Parameters:

  • This method does not take any parameters.

Returns:

  • An OptionalDouble containing the average value of the elements of the stream, or an empty OptionalDouble if the stream is empty.

Throws:

  • This method does not throw any exceptions.

Understanding average()

The average() method computes the average value of all elements in a DoubleStream. If the stream is empty, it returns an empty OptionalDouble, which can be checked to determine if a value is present.

Examples

Basic Usage

To demonstrate the basic usage of average(), we will create a DoubleStream and calculate the average of its elements.

Example

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

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

        // Calculate the average of the numbers
        OptionalDouble average = numbers.average();

        average.ifPresent(avg -> System.out.println("Average: " + avg));
    }
}

Output:

Average: 3.3

Handling an Empty Stream

This example shows how to handle an empty stream when using the average() method.

Example

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

public class EmptyStreamExample {
    public static void main(String[] args) {
        DoubleStream emptyStream = DoubleStream.empty();

        // Calculate the average of the empty stream
        OptionalDouble average = emptyStream.average();

        if (average.isPresent()) {
            System.out.println("Average: " + average.getAsDouble());
        } else {
            System.out.println("The stream is empty, no average value.");
        }
    }
}

Output:

The stream is empty, no average value.

Real-World Use Case

Calculating Average Temperature

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

Example

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

public class TemperatureAverageExample {
    public static void main(String[] args) {
        DoubleStream temperatures = DoubleStream.of(36.5, 37.0, 36.8, 37.1, 36.9);

        // Calculate the average temperature
        OptionalDouble averageTemperature = temperatures.average();

        averageTemperature.ifPresent(avg -> System.out.println("Average Temperature: " + avg));
    }
}

Output:

Average Temperature: 36.86

Conclusion

The DoubleStream.average() method is used to calculate the average of the elements in a stream. This method is particularly useful for computing the average value of a sequence of double values. By understanding and using this method, you can efficiently perform average calculations on streams of double values in your Java applications.

Comments