如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:52:57  来源:igfitidea点击:

How to flatmap a stream of streams in Java?

javajava-8java-stream

提问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 flatMapmethod, 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.

因为你必须为每个流成员提供一些映射函数,在这种情况下它是标识函数。