空列表上的Java 8流操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54501501/
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 01:03:32  来源:igfitidea点击:

Java 8 stream operation on empty list

javajava-8java-stream

提问by gaurav agarwal

I am just wondering what will be behavior of Java 8 streamon empty list.

我只是想知道 Java 8在空列表上的行为是什么。

List<?> emptyList = new ArrayList<>();
List<?> processedList = emptyList.stream().collect(Collectors.toList());

Will this be empty list or null?

这将是空列表还是null

I know streamsdo lazy propagation, so in this case will call go to collect()method or just it will end at stream()method?

我知道流会进行延迟传播,因此在这种情况下将调用 go tocollect()方法还是仅以stream()方法结束?

采纳答案by Eran

collectis a terminal operation, so it must be evaluated.

collect是一个终端操作,因此必须对其进行评估。

When terminating a Streampipeline with collect(Collectors.toList()), you'll always get an output List(you'll never get null). If the Streamis empty (and it doesn't matter if it's empty due to the source of the stream being empty, or due to all the elements of the stream being filtered out prior to the terminal operation), the output Listwill be empty too.

使用 终止Stream管道时collect(Collectors.toList()),您将始终获得输出List(您永远不会获得null)。如果Stream为空(并且由于流的源为空,或者由于流的所有元素在终端操作之前被过滤掉而为空都无关紧要),输出List也将为空。

回答by Lebecca

You will get a empty collection. As collectis explained in doc:

你会得到一个空的集合。正如doc 中解释的collect

Performs a mutable reduction operation on the elements of this stream using a Collector.

使用收集器对此流的元素执行可变归约操作。

and the mutable reduction:

可变减少

A mutable reduction operation accumulates input elements into a mutable result container, such as a Collection or StringBuilder, as it processes the elements in the stream.

可变归约操作在处理流中的元素时将输入元素累积到可变结果容器中,例如 Collection 或 StringBuilder。

You will get a empty collection because of the origin input is empty or due to the filter operation.

由于原始输入为空或由于过滤操作,您将获得一个空集合。

Thanks for @Andy Turner 's tips.

感谢@Andy Turner 的提示。

It's the fact that toList() accumulates into a new list that means it doesn't return null.

事实上 toList() 会累积到一个新列表中,这意味着它不会返回 null。

And the doc gets explain for the Collectors.toList() with this:

并且该文档通过以下方式对 Collectors.toList() 进行了解释:

Returns a Collector that accumulates the input elements into a new List.

返回一个将输入元素累积到新列表中的收集器。

We can get from the source code

我们可以从源代码中得到

    public static <T>
    Collector<T, ?, List<T>> toList() {
        return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add,
                                   (left, right) -> { left.addAll(right); return left; },
                                   CH_ID);
    }

that it will never make a null output but you can still get a null with customized Collector as Andy shows.

它永远不会产生空输出,但你仍然可以像安迪展示的那样使用定制的收集器获得空值。