Java 8 Collector UNORDERED特性是什么意思? [英] What does the Java 8 Collector UNORDERED characteristic mean?

查看:229
本文介绍了Java 8 Collector UNORDERED特性是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在官方文档中,您可以阅读:

In official documentation you can read that:


UNORDERED
表示集合操作未提交
,保留输入元素的遭遇顺序。

UNORDERED Indicates that the collection operation does not commit to preserving the encounter order of input elements.

这不太有帮助没有任何例子。

This is not too helpful without any examples.

我的问题是, UNORDERED 具体意味着什么?我应该使用它来减少收集器,如min或sum,还是只适用于收集器?

My question is, what exactly does UNORDERED characteristic mean? Should I use it with reducing collectors like min or sum or is it only applicable to collection collectors?

在OpenJDK看起来像减少操作(min,sum,avg)是空的特点。我希望在那里找到至少 CONCURRENT UNORDERED

In OpenJDK looks like reducing operations (min, sum, avg) have empty characteristics. I expected to find there at least CONCURRENT and UNORDERED.

推荐答案

UNORDERED 本质上意味着收集器既是关联的(规范要求)又可交换的(不是必需的)。

UNORDERED essentially means that the collector is both associative (required by the spec) and commutative (not required).

关联性允许将计算分成子部分,然后将它们组合成完整的结果,但需要严格排序组合步骤。从文档中检查此代码段:

Associativity allows splitting the computation into subparts and then combining them into the full result, but requires the combining step to be strictly ordered. Examine this snippet from the docs:

 A a2 = supplier.get();
 accumulator.accept(a2, t1);
 A a3 = supplier.get();
 accumulator.accept(a3, t2);
 R r2 = finisher.apply(combiner.apply(a2, a3));  // result with splitting

在最后一步中, combiner.apply(a2 ,a3),参数必须以这个顺序出现,这意味着整个计算管道必须跟踪订单并最终尊重它。

In the last step, combiner.apply(a2, a3), the arguments must appear in exactly this order, which means that the entire computation pipeline must track the order and respect it in the end.

另一种说法是必须对从递归拆分中得到的树进行排序。

Another way of saying this is that the tree we get from recursive splitting must be ordered.

另一方面,如果组合操作是可交换的,我们可以将任何子部分与任何其他部分组合,无需特定顺序,并始终获得相同的结果。显然,这会在空间和时间维度上带来许多优化机会。

On the other hand, if the combining operation is commutative, we can combine any subpart with any other, in no particular order, and always obtain the same result. Clearly this leads to many optimization opportunities in both space and time dimensions.

应该注意的是, UNORDERED JDK中的收集器不保证交换性。主要类别是由其他下游收集器组成的高阶收集器,但它们不强制执行 UNORDERED 属性。

It should be noted that there are UNORDERED collectors in the JDK which don't guarantee commutativity. The main category are the "higher-order" collectors which are composed with other downstream collectors, but they don't enforce the UNORDERED property on them.

这篇关于Java 8 Collector UNORDERED特性是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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