Java DoubleStream of() Method

The of() method in Java, part of the java.util.stream.DoubleStream interface, is used to create a DoubleStream from specified double values. This method is useful when you need to quickly create a DoubleStream with known elements.

Table of Contents

  1. Introduction
  2. of() Method Syntax
  3. Understanding of()
  4. Examples
    • Basic Usage
    • Creating a Stream from an Array of Doubles
  5. Real-World Use Case
  6. Conclusion

Introduction

The of() method is a static method that allows you to create a DoubleStream containing the specified double values. This method provides a convenient way to create a stream from a fixed set of elements.

of() Method Syntax

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

static DoubleStream of(double... values)

Parameters:

  • values: A varargs parameter representing the double values to be included in the stream.

Returns:

  • A DoubleStream containing the specified double values.

Throws:

  • This method does not throw any exceptions.

Understanding of()

The of() method is a straightforward way to create a DoubleStream with known elements. It can take any number of double values as arguments and returns a stream containing those values.

Examples

Basic Usage

To demonstrate the basic usage of of(), we will create a DoubleStream with a few double values and print them.

Example

import java.util.stream.DoubleStream;

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

        // Print the elements of the stream
        doubleStream.forEach(System.out::println);
    }
}

Output:

1.1
2.2
3.3
4.4
5.5

Creating a Stream from an Array of Doubles

This example shows how to create a DoubleStream from an array of doubles using the of() method.

Example

import java.util.stream.DoubleStream;

public class ArrayExample {
    public static void main(String[] args) {
        double[] values = {6.6, 7.7, 8.8, 9.9, 10.10};

        // Create a DoubleStream from an array of doubles
        DoubleStream doubleStream = DoubleStream.of(values);

        // Print the elements of the stream
        doubleStream.forEach(System.out::println);
    }
}

Output:

6.6
7.7
8.8
9.9
10.1

Real-World Use Case

Initializing Sensor Data Stream

In real-world applications, the of() method can be used to initialize a stream of sensor data for processing.

Example

import java.util.stream.DoubleStream;

public class SensorDataExample {
    public static void main(String[] args) {
        // Initialize sensor data stream
        DoubleStream sensorData = DoubleStream.of(25.3, 26.7, 24.8, 27.5, 30.1);

        // Process the sensor data stream
        sensorData.forEach(reading -> System.out.println("Sensor reading: " + reading));
    }
}

Output:

Sensor reading: 25.3
Sensor reading: 26.7
Sensor reading: 24.8
Sensor reading: 27.5
Sensor reading: 30.1

Conclusion

The DoubleStream.of() method is used to create a DoubleStream from specified double values. This method is particularly useful for quickly creating streams with known elements. By understanding and using this method, you can efficiently initialize and manipulate streams of double values in your Java applications.

Comments