How to flatmap a stream of streams in Java?

JavaJava 8Java Stream

Java Problem Overview


I want to transform the stream of streams of objects to a single stream of objects. I know that I have to use the flatMap method, but I'm not able to achieve that, look at:

Stream<Stream<Object>> objectStreams = ...
Stream<Object> flatMappedStream = objectStreams.flatMap( ... );

Could anyone please help me?

Java Solutions


Solution 1 - Java

Basically, you want to concatenate all the nested streams into one flat stream, without affecting the members themselves. You'll use

objectStreams.flatMap(Function.identity());

because you must provide some mapping function for each stream member, and in this case it is the identity function.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwinklerrrView Question on Stackoverflow
Solution 1 - JavaMarko TopolnikView Answer on Stackoverflow