Java DoubleStream flatMap() Method

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The flatMap() method in Java, part of the java.util.stream.DoubleStream interface, is used to transform each element of a stream into another stream and then flatten the resulting streams into a single stream. This method is useful when you need to handle nested streams and merge them into a single continuous stream.

Table of Contents

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

Introduction

The flatMap() method returns a stream consisting of the results of replacing each element of the stream with the contents of a mapped stream produced by applying a provided mapping function to each element. This method is useful for handling nested streams and merging them into a single continuous stream.

flatMap() Method Syntax

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

DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper)

Parameters:

  • mapper: A stateless function that is applied to each element, which produces a stream of new values.

Returns:

  • A new DoubleStream consisting of the results of replacing each element of this stream with the contents of the mapped stream.

Throws:

  • This method does not throw any exceptions.

Understanding flatMap()

The flatMap() method allows you to take each element of a DoubleStream, transform it into another DoubleStream, and then flatten these streams into a single stream. This is particularly useful for dealing with nested streams or collections of collections.

Examples

Basic Usage

To demonstrate the basic usage of flatMap(), we will create a DoubleStream and use flatMap() to transform each element into a stream of its square and cube.

Example

import java.util.stream.DoubleStream;

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

        // Use flatMap to transform each element into a stream of its square and cube
        DoubleStream transformedStream = doubleStream.flatMap(n -> DoubleStream.of(n * n, n * n * n));

        // Print the transformed stream
        transformedStream.forEach(System.out::println);
    }
}

Output:

1.0
1.0
4.0
8.0
9.0
27.0

Using flatMap() with Custom Transformation

This example shows how to use flatMap() to transform a stream of numbers into a stream of ranges.

Example

import java.util.stream.DoubleStream;

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

        // Use flatMap to transform each element into a range from 0 to that element (exclusive)
        DoubleStream transformedStream = doubleStream.flatMap(n -> DoubleStream.iterate(0.0, i -> i + 1.0).limit((long)n));

        // Print the transformed stream
        transformedStream.forEach(System.out::println);
    }
}

Output:

0.0
0.0
1.0
0.0
1.0
2.0

Real-World Use Case

Flattening Nested Streams of Sensor Data

In real-world applications, the flatMap() method can be used to flatten nested streams of sensor data into a single stream for processing.

Example

import java.util.stream.DoubleStream;
import java.util.stream.Stream;

public class SensorDataFlatMapExample {
    public static void main(String[] args) {
        Stream<DoubleStream> sensorDataStreams = Stream.of(
            DoubleStream.of(25.3, 26.7),
            DoubleStream.of(24.8, 27.5),
            DoubleStream.of(30.1, 29.4)
        );

        // Use flatMap to flatten the nested streams into a single stream
        DoubleStream flattenedStream = sensorDataStreams.flatMapToDouble(ds -> ds);

        // Print the flattened stream
        flattenedStream.forEach(System.out::println);
    }
}

Output:

25.3
26.7
24.8
27.5
30.1
29.4

Conclusion

The DoubleStream.flatMap() method is used to transform each element of a stream into another stream and then flatten the resulting streams into a single stream. This method is particularly useful for handling nested streams and merging them into a single continuous stream. By understanding and using this method, you can efficiently manage and manipulate complex data structures in your Java applications.

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare