Java 的 Stream.collect() 可以返回 null 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50256720/
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
Can Java's Stream.collect() return null?
提问by Dean Schulze
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:
Stream.collect() 的 JavaDoc 说它返回the result of the reduction
. 这并没有告诉我这样的代码是否可以为filteredList返回null:
List<String> filteredList = inputList.stream().
filter(c -> c.getSomeBoolean()).
flatMap(c -> {
List<String> l = new ArrayList<String>();
l.add(c.getSomething());
l.add(c.getSomethingElse());
return l.stream();
}).
filter(s -> StringUtils.isNotBlank(s)).
collect(Collectors.toList());
I would expect that if it could return null then it would return Optional, but it doesn't say that either.
我希望如果它可以返回 null 那么它会返回 Optional,但它也没有这么说。
Is it documented anywhere whether Stream.collect() can return null?
Stream.collect() 是否可以返回 null 是否有记录?
采纳答案by M?nh Quy?t Nguy?n
Collector.toList()
will return an emptyList for you.
Collector.toList()
将为您返回一个空列表。
Here is the implementation:
这是实现:
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);
}
As you can see ArrayList::new
is being used as a container for your items.
正如您所看到的,ArrayList::new
它被用作您物品的容器。
From JavaDoc of Collector:
来自Collector 的JavaDoc :
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:
creation of a new result container (supplier())
incorporating a new data element into a result container (accumulator())
- combining two result containers into one (combiner())
- performing an optional final transform on the container (finisher())
将输入元素累积到可变结果容器中的可变归约操作 ,可选择在处理完所有输入元素后将累积结果转换为最终表示。归约操作可以顺序执行,也可以并行执行。
收集器由四个函数指定,这些函数一起工作以将条目累积到可变结果容器中,并可选择对结果执行最终转换。他们是:
创建新的结果容器 (supplier())
将新数据元素合并到结果容器中 (accumulator())
- 将两个结果容器合二为一(combiner())
- 对容器执行可选的最终转换(finisher())
And
和
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 resultcontainer 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.
使用收集器按顺序实现归约将 使用供应商函数创建单个结果容器,并为每个输入元素调用累加器函数一次。并行实现将输入分区,为每个分区创建一个结果容器,将每个分区的内容累积到该分区的子结果中,然后使用组合器函数将子结果合并为组合结果。
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.
所以只要你不做一些奇怪的事情,比如 combine function return null
,Collector
总是mutable container
使用你提供的supplier
函数至少返回 a 。
And I think it's very counter-intuitive if an implementation would ever return null
container.
而且我认为如果实现会返回null
容器,这是非常违反直觉的。
回答by Jan Ossowski
This is collector-dependant. The one You're using (Collectors.toList()) returns an empty list.
这取决于收集器。您正在使用的 (Collectors.toList()) 返回一个空列表。
回答by Michael
This is not dependent on Stream.collect
, but on the individual Collector
. Collectors.toList()
will return an empty ArrayList
.
这不是取决于Stream.collect
,而是取决于个人Collector
。Collectors.toList()
将返回一个空的ArrayList
.
That said, there's no reason someone couldn't use a weird Collector
to return null in certain circumstances:
也就是说,Collector
在某些情况下,没有理由不能使用奇怪的方法返回 null:
.collect(
Collector.of(
ArrayList::new,
ArrayList::add,
(a, b) -> {
a.addAll(b);
return a;
},
a -> a.isEmpty() ? null : a // finisher replaces empty list with null
)
);
So the Collector
is the thing you need to remember to check. I believe all of the Collectors
available out-of-the-boxwill return empty collections, as you'd expect.
所以这Collector
是你需要记住检查的事情。我相信所有Collectors
可用的开箱即用都将返回空集合,正如您所期望的那样。
回答by Tamas Rev
I think this part of the documentation says that it cannot be null:
我认为文档的这一部分说它不能为空:
Returns a Collector that accumulates the input elements into a new List.
返回一个收藏家,其累积的输入元素为NEW¯¯ 列表。
Highlightsadded by me. I think this new Listmeans that something that isn't null.
我添加的亮点。我认为这个新列表意味着不为空的东西。
I started to check ReferencePipeline.collect()
to check whether it's true for the actual implementation. Unfortunately, it was a futile attempt. There are so many cases here, like is it parallel? is it after a forEach
? etc.
我开始检查ReferencePipeline.collect()
以检查实际实现是否正确。不幸的是,这是一次徒劳的尝试。这里的案例太多了,好像是平行的?是在 a 之后forEach
吗?等等。