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

编程入门 行业动态 更新时间:2024-10-26 04:19:37
本文介绍了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?

本文发布于:2023-11-10 03:05:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Stream   Java   null   collect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!