Java SE API
Stream.flatMap()
Flattens mapped streams, which are closed after use. Java SE API; example uses Java 21-compatible syntax.
Formal Syntax & Structure
java syntaxjava
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| mapper | Function<? super T, ? extends Stream<? extends R>> | Stream-producing transformation |
Return Value
Stream<R>
Flattens mapped streams, which are closed after use.
Working Example
Example Snippetjava
import java.util.*;import java.util.stream.*;public class Main { public static void main(String[] args) { var shipments = List.of(List.of("A", "B"), List.<String>of(), List.of("C")); System.out.println(shipments.stream().flatMap(List::stream).toList()); }}