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]

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz