🎓 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
- Introduction
flatMap()Method Syntax- Understanding
flatMap() - Examples
- Basic Usage
- Using
flatMap()with Custom Transformation
- Real-World Use Case
- 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
DoubleStreamconsisting 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:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment