且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在一行中获取流/列表的最后一个元素

更新时间:2023-11-10 08:22:58

有可能获得最后一个元素使用方法 Stream: :减少。以下列表包含一般情况的最小示例:

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:


  • 方法的Javadoc Stream :: reduce 状态,它约束执行顺序

  • Javadoc还要求累加器函数必须是关联非干扰无状态函数,用于组合两个值,这显然是lambda表达式的案例(第一,第二) - >第二个

  • 简化操作声明:流类有多种形式的常规缩减操作,称为 reduce() collect() [..]正确构造的reduce操作固有可并行化,只要用于处理元素的函数是associative 无状态

  • The Javadoc for the method Stream::reduce states, that it "is not constrained to execute sequentially".
  • The Javadoc also requires that the "accumulator function must be an associative, non-interfering, stateless function for combining two values", which is obviously the case for the lambda expression (first, second) -> second.
  • The Javadoc for reduction operations states: "The streams classes have multiple forms of general reduction operations, called reduce() and collect() [..]" and "a properly constructed reduce operation is inherently parallelizable, so long as the function(s) used to process the elements are associative and stateless."

该文件密切相关的收藏家更加明确: 为确保顺序并行执行生成等效结果,收集器函数必须满足标识并且associativity 约束。

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