更新时间:2023-11-10 08:04:40
可以通过Stream::reduce.以下清单包含一般情况的最小示例:
It is possible to get the last element with the method Stream::reduce. The following listing contains a minimal example for the general case:
Stream<T> stream = ...; // sequential or parallel stream
Optional<T> last = stream.reduce((first, second) -> second);
此实现适用于所有 有序流(包括从 列表创建的流).对于 无序 流出于明显的原因,未指定将返回哪个元素.
This implementations works for all ordered streams (including streams created from Lists). For unordered streams it is for obvious reasons unspecified which element will be returned.
该实现适用于顺序和并行流.乍一看,这可能令人惊讶,不幸的是,文档没有明确说明.但是,它是流的一个重要特性,我试图澄清它:
The implementation works for both sequential and parallel streams. That might be surprising at first glance, and unfortunately the documentation doesn't state it explicitly. However, it is an important feature of streams, and I try to clarify it:
(first, second) -> 的情况.第二个
.(first, second) -> second
.密切相关的文档 收集器 更加明确:为确保顺序和并行执行产生等效结果,收集器函数必须满足身份和 关联性 约束."
The documentation for the closely related Collectors is even more explicit: "To ensure that sequential and parallel executions produce equivalent results, the collector functions must satisfy an identity and an associativity constraints."
回到原来的问题:下面的代码在变量last
中存储了对最后一个元素的引用,如果流为空则抛出异常.复杂度与流的长度成线性关系.
Back to the original question: The following code stores a reference to the last element in the variable last
and throws an exception if the stream is empty. The complexity is linear in the length of the stream.
CArea last = data.careas
.stream()
.filter(c -> c.bbox.orientationHorizontal)
.reduce((first, second) -> second).get();