如何在 Java 中平面映射流?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31992290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to flatmap a stream of streams in Java?
提问by winklerrr
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:
我想将对象流转换为单个对象流。我知道我必须使用该flatMap
方法,但我无法实现,请查看:
Stream<Stream<Object>> objectStreams = ...
Stream<Object> flatMappedStream = objectStreams.flatMap( ... );
Could anyone please help me?
有人可以帮我吗?
采纳答案by Marko Topolnik
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 somemapping function for each stream member, and in this case it is the identity function.
因为你必须为每个流成员提供一些映射函数,在这种情况下它是标识函数。