Java的Stream.collect()是否可以返回null? [英] Can Java's Stream.collect() return null?

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

问题描述

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

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()将为您返回一个列表.

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.

来自收藏家的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())

还有

使用收集器顺序执行归约将 使用供应商功能创建单个结果容器,然后 为每个输入元素调用一次累加器函数.一种 并行实现将对输入进行分区,创建结果 每个分区的容器,累积每个分区的内容 分区到该分区的子结果中,然后使用 合并器功能,可将子结果合并为合并结果.

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.

只要您不执行组合功能返回null之类的奇怪操作,Collector始终使用提供的supplier函数至少返回一个mutable container.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆