Java DoubleStream map() Method

The map() method in Java, part of the java.util.stream.DoubleStream interface, is used to apply a given function to each element of the stream, producing a new DoubleStream with the results of the applied function. This method is useful when you need to transform the elements of a stream.

Table of Contents

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

Introduction

The map() method returns a stream consisting of the results of applying a given function to the elements of the original stream. This method is an intermediate operation, meaning it returns a new stream and does not modify the original stream.

map() Method Syntax

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

DoubleStream map(DoubleUnaryOperator mapper)

Parameters:

  • mapper: A DoubleUnaryOperator that represents the function to be applied to each element of the stream.

Returns:

  • A new DoubleStream consisting of the results of applying the given function to the elements of the original stream.

Throws:

  • This method does not throw any exceptions.

Understanding map()

The map() method allows you to transform the elements of a DoubleStream by applying a specified function to each element. The resulting stream contains the transformed elements, preserving the order of the original stream.

Examples

Basic Usage

To demonstrate the basic usage of map(), we will create a DoubleStream and use map() to square each element of the stream.

Example

import java.util.stream.DoubleStream;

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

        // Apply a function to square each element of the stream
        DoubleStream squaredStream = doubleStream.map(n -> n * n);

        // Print the squared stream
        squaredStream.forEach(System.out::println);
    }
}

Output:

1.2100000000000002
4.840000000000001
10.889999999999999
19.360000000000003
30.25

Using map() with Mathematical Operations

This example shows how to use map() to apply various mathematical operations to the elements of a DoubleStream.

Example

import java.util.stream.DoubleStream;

public class MathOperationsExample {
    public static void main(String[] args) {
        DoubleStream doubleStream = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0);

        // Apply a function to double each element of the stream
        DoubleStream doubledStream = doubleStream.map(n -> n * 2);

        // Print the doubled stream
        doubledStream.forEach(System.out::println);
    }
}

Output:

2.0
4.0
6.0
8.0
10.0

Real-World Use Case

Converting Sensor Readings

In real-world applications, the map() method can be used to convert sensor readings from one unit to another. For example, converting temperatures from Celsius to Fahrenheit.

Example

import java.util.stream.DoubleStream;

public class SensorDataConversionExample {
    public static void main(String[] args) {
        DoubleStream celsiusReadings = DoubleStream.of(25.0, 30.0, 20.0, 15.0);

        // Convert the sensor readings from Celsius to Fahrenheit
        DoubleStream fahrenheitReadings = celsiusReadings.map(celsius -> celsius * 9 / 5 + 32);

        // Print the converted readings
        fahrenheitReadings.forEach(fahrenheit -> System.out.println("Fahrenheit reading: " + fahrenheit));
    }
}

Output:

Fahrenheit reading: 77.0
Fahrenheit reading: 86.0
Fahrenheit reading: 68.0
Fahrenheit reading: 59.0

Conclusion

The DoubleStream.map() method is used to apply a given function to each element of the stream, producing a new stream with the results of the applied function. This method is particularly useful for transforming the elements of a stream. By understanding and using this method, you can efficiently manage and manipulate streams of double values in your Java applications.

Comments