以单行方式获取 Stream/List 的最后一个元素 [英] Get last element of Stream/List in a one-liner

查看:68
本文介绍了以单行方式获取 Stream/List 的最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取以下代码中流或列表的最后一个元素?

How can I get the last element of a stream or list in the following code?

其中 data.careas 是一个 List:

CArea first = data.careas.stream()
                  .filter(c -> c.bbox.orientationHorizontal).findFirst().get();

CArea last = data.careas.stream()
                 .filter(c -> c.bbox.orientationHorizontal)
                 .collect(Collectors.toList()).; //how to?

如您所见,使用特定的 filter 获取第一个元素并不难.

As you can see getting the first element, with a certain filter, is not hard.

然而,在单行中获取最后一个元素是一个真正的痛苦:

However getting the last element in a one-liner is a real pain:

  • 似乎我无法直接从 Stream 获取它.(它只对有限流有意义)
  • 好像也无法从List接口获取first()last()之类的东西,真的很痛苦.
  • It seems I cannot obtain it directly from a Stream. (It would only make sense for finite streams)
  • It also seems that you cannot get things like first() and last() from the List interface, which is really a pain.

我没有看到在 List 接口中没有提供 first()last() 方法作为元素的任何论据在那里,是有序的,而且尺寸是已知的.

I do not see any argument for not providing a first() and last() method in the List interface, as the elements in there, are ordered, and moreover the size is known.

但根据原始答案:如何获取有限 Stream 的最后一个元素?

But as per the original answer: How to get the last element of a finite Stream?

就个人而言,这是我能得到的最接近的:

Personally, this is the closest I could get:

int lastIndex = data.careas.stream()
        .filter(c -> c.bbox.orientationHorizontal)
        .mapToInt(c -> data.careas.indexOf(c)).max().getAsInt();
CArea last = data.careas.get(lastIndex);

但是它确实涉及在每个元素上使用 indexOf,这很可能不是您通常想要的,因为它会影响性能.

However it does involve, using an indexOf on every element, which is most likely not you generally want as it can impair performance.

推荐答案

可以使用方法 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:

  • 方法的 Javadoc Stream::reduce 声明,它不受约束以顺序"执行.
  • Javadoc 还要求 "accumulator 函数必须是一个关联非干扰无状态函数,用于组合两个values",这显然是 lambda 表达式的情况 (first, second) ->第二个.
  • 减少的 Javadoc操作指出:流类有多种形式的通用归约操作,称为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."

密切相关的收集器的文档 更加明确:为了确保顺序并行执行产生等效结果,收集器函数必须满足身份和 关联性 约束."

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

这篇关于以单行方式获取 Stream/List 的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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