Java Stream flatMap() Example
Tags: java
Published: 2021-12-09 11:21:40

The Java Stream _flatMap()_ method is an intermediate operation.

The _Stream.flatMap()_ function, as the name suggests, is the combination of a map and a flat operation. This means you first apply the map function and then flatten the result. 

## Java Stream flatMap() Example
To understand what flattening a stream consists in, consider a structure like [ [1,2,3],[4,5,6],[7,8,9] ] which has "two levels". It's basically a big List containing three more List. Flattening this means transforming it in a "one level" structure e.g. [ 1,2,3,4,5,6,7,8,9 ] i.e. just one list.

**For example:** In the below program, you can see that we have three lists that are merged into one by using a flatMap() function:
```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<Integer> evens = Arrays.asList(2, 4, 6);
        List<Integer> odds = Arrays.asList(3, 5, 7);
        List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11);
        List<Integer> numbers = Stream.of(evens, odds, primes)
                .flatMap(list -> list.stream())
                .collect(Collectors.toList());
        System.out.println("flattend list: " + numbers);
    }
}
```
Output:
```
flattend list: [2, 4, 6, 3, 5, 7, 2, 3, 5, 7, 11]
```
## Related Java Stream API's Examples
<ol><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-filter-example.html" target="_blank">Java Stream filter() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-map-example.html" target="_blank">Java Stream map() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-flatmap-example.html" target="_blank">Java Stream flatMap() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-distinct-example.html" target="_blank">Java Stream distinct() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-limit-example.html" target="_blank">Java Stream limit() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-peek-example.html" target="_blank">Java Stream peek() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-anymatch-example.html" target="_blank">Java Stream anyMatch() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-allmatch-example.html" target="_blank">Java Stream allMatch() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-nonematch-example.html" target="_blank">Java Stream noneMatch() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-collect-example.html" target="_blank">Java Stream collect() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-count-example.html" target="_blank">Java Stream count() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-findany-example.html" target="_blank">Java Stream findAny() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-findfirst-example.html" target="_blank">Java Stream findFirst() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-foreach-example.html" target="_blank">Java Stream forEach() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-min-example.html" target="_blank">Java Stream min() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-max-example.html" target="_blank">Java Stream max() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-reduce-example.html" target="_blank">Java Stream reduce() Example</a></li><li><a href="https://www.sourcecodeexamples.net/2021/11/java-stream-toarray-example.html" target="_blank">Java Stream toArray() Example</a></li></ol>