Flatten a Stream in Java 8 using forEach() method

We have already covered how to flatten a Stream using flatMap() and concat() methods in Java 8. In this post, we will flatten a Stream in Java 8 using Stream.forEach() method.


 

1. Flatten a Stream of Arrays

 

Download   Run Code

Output:

[A, B, C, D, E, F, G]

 

2. Flatten a Stream of Lists

 

Download   Run Code

Output:

[1, 2, 3, 4, 5, 6, 7]

 

3. Flatten a Map

 

Download   Run Code

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of