Java Stream flatMap() Tutorial

beginner
13 min

Java Stream flatMap() Tutorial

Welcome to our comprehensive guide on the flatMap() method in Java Stream API! In this lesson, we'll dive deep into understanding this powerful function, learn how it works, and see some practical examples.

By the end of this tutorial, you'll be confident in using flatMap() to transform your Java Streams and make your code more efficient. Let's get started! 🎯

What is flatMap() in Java Stream API?

flatMap() is a method in the Java Stream API that lets you apply a provided function to each element of a Stream and collect the resulting Streams into a single Stream. This is particularly useful when you want to transform the elements of a Stream and then process the resulting Streams as a whole.

Breaking it Down

Let's break down flatMap():

  • flat: Combines multiple Streams into one Stream
  • map: Applies a function to each element of the Stream

Why Use flatMap()?

flatMap() is beneficial when dealing with collections of collections or when performing complex transformations on individual elements. By using flatMap(), you can simplify your code, making it more readable and maintainable.

How to Use flatMap()

To use flatMap(), follow these steps:

  1. Import the required package:
java
import java.util.stream.Collectors; import java.util.stream.Stream;
  1. Define your Stream of Streams:
java
List<List<String>> listOfLists = Arrays.asList( Arrays.asList("apple", "banana", "cherry"), Arrays.asList("grape", "kiwi", "lemon"), Arrays.asList("mango", "orange", "pear") ); Stream<Stream<String>> listStream = listOfLists.stream();
  1. Use the flatMap() method:
java
Stream<String> singleStream = listStream.flatMap(Stream::of);

In the example above, we have a list of lists containing fruit names. By using flatMap() with Stream::of, we combine the lists into a single Stream containing all the fruit names.

Pro Tip:

When using flatMap(), you can provide a custom function to perform the transformation on the elements.

Advanced Example

Let's consider a more practical example: We have a list of Student objects, and each student has a list of subjects. Using flatMap(), we can create a single Stream containing all the subjects.

java
class Student { private String name; private List<String> subjects; // getters and setters } List<Student> students = Arrays.asList( new Student("Alice", Arrays.asList("Math", "English", "Science")), new Student("Bob", Arrays.asList("History", "Computer Science", "Art")), new Student("Charlie", Arrays.asList("Physics", "French", "Geography")) ); Stream<String> subjectStream = students.stream() .flatMap(student -> student.getSubjects().stream());

In the example above, we have a list of Student objects, and each student has a list of subjects. By using flatMap(), we combine the lists of subjects into a single Stream containing all the subjects.

Quiz Time!

Quick Quiz
Question 1 of 1

What does the `flatMap()` method in Java Stream API do?

That's it for this lesson! In the next lesson, we'll dive into using flatMapToInt() and flatMapToLong() to perform mathematical operations on a Stream of Streams.

Stay tuned, and happy coding! 💡📝🎯