Java 的 Stream.collect() 可以返回 null 吗?

Can Java#39;s Stream.collect() return null?(Java 的 Stream.collect() 可以返回 null 吗?)

本文介绍了Java 的 Stream.collect() 可以返回 null 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stream.collect() 的 JavaDoc 说它返回归约的结果".这并不能告诉我这样的代码是否可以为 filteredList 返回 null:

The JavaDoc for Stream.collect() says that it returns "the result of the reduction". That doesn't tell me if code like this can return null for filteredList:

List<String> filteredList = inputList.stream()
    .filter(c -> c.isActive())
    .collect(Collectors.toList());

我希望如果它可以返回 null,那么它会返回一个 Optional,但它也没有这么说.

I would expect that if it could return null then it would return an Optional, but it doesn't say that either.

是否记录了 Stream.collect() 是否可以返回 null?

Is it documented anywhere whether Stream.collect() can return null?

推荐答案

Collector.toList() 将为您返回一个 empty 列表.

Collector.toList() will return an empty List for you.

这里是实现:

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);
}

如您所见,ArrayList::new 被用作您的项目的容器.

As you can see ArrayList::new is being used as a container for your items.

来自 Collector 的 JavaDoc:

From JavaDoc of Collector:

可变归约运算将输入元素累积到可变结果容器中,可选之后将累积的结果转换为最终表示所有输入元素都已处理.减少操作可以顺序或并行执行.

A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed. Reduction operations can be performed either sequentially or in parallel.

收集器由四个共同作用的函数指定将条目累积到可变结果容器中,并且可以选择对结果执行最终转换.它们是:

A Collector is specified by four functions that work together to accumulate entries into a mutable result container, and optionally perform a final transform on the result. They are:

  • 创建新的结果容器(supplier())

将新数据元素合并到结果容器中(accumulator())

incorporating a new data element into a result container (accumulator())

使用收集器的减少的顺序实现将使用供应商函数创建单个结果容器,并且为每个输入元素调用一次累加器函数.一个并行实现会对输入进行分区,创建结果每个分区的容器,累加每个分区的内容分区为该分区的子结果,然后使用combiner 函数将子结果合并成一个组合结果.

A sequential implementation of a reduction using a collector would create a single result container using the supplier function, and invoke the accumulator function once for each input element. A parallel implementation would partition the input, create a result container for each partition, accumulate the contents of each partition into a subresult for that partition, and then use the combiner function to merge the subresults into a combined result.

所以只要你不做一些奇怪的事情,比如组合函数返回 nullCollector 总是至少返回一个 可变容器使用您提供的 supplier 函数.

So as long as you don't do weird things like combine function return null, the Collector always return at least a mutable container using your provided supplier function.

而且我认为如果一个实现会返回 null 容器是非常违反直觉的.

And I think it's very counter-intuitive if an implementation would ever return null container.

这篇关于Java 的 Stream.collect() 可以返回 null 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Java 的 Stream.collect() 可以返回 null 吗?

基础教程推荐