flatMap with example in Java 8

flatMap with example in Java 8:

To know flatMap with example in Java 8 . please go for below steps.

Stream can hold different data types In Java 8 like below example:

 

But, the Collectors and Stream operations (distinct,filter, sum,…) do not support it, so, we need flatMap() to do the following conversion :

 

How flatMap() Perform:

1. flatMap,Stream and String[]

1.1 The below example will print an empty result, because filter() has no idea how to filter a stream of String[].

 

1.2 To convert Stream<String[]> to Stream<String> we should use flatMap() ,In above example.

 

2. flatMapToInt,Stream and Primitive

2.1 We can use flatMapToInt For primitive type.

 

2. flatMap,Stream and Set

2.1 POJO for a Pepsodent.

 

2.2 flatMap() and Set example.

 

Facebook