有序流上的减少是按顺序减少的吗?

问题描述:

我有一个列表的A,B,C。

I have a List of A, B, C.

C减少A减少B!= A减少B减少C (但是,减少(B减少C)是可以的)。

C reduce A reduce B != A reduce B reduce C (however, A reduce (B reduce C) is OK).

换句话说,我的缩减操作是关联的但不是可交换的。

In other words, my reduction operation is associative but not commutative.

java是否对有序顺序执行流(例如列表中的默认值)是否会根据遭遇顺序进行减少?也就是说,java会重新排序减少(这样B减少A而不是A减少B)?

Does java enforce on an ordered sequential stream (such as the default one from a list) that reduction will always happen according to the encounter order? That is to say, will java reorder reductions (such that B reduce A instead of A reduce B)?

(希望这很清楚)。

编辑添加一个小演示,可能有助于澄清

edit to add a little demo and maybe helping to clarify

Stream.of(" cats ", " eat ", " bats ")
  .reduce("", (a, b) -> a + b); // cats eat bats

有了上述,输出可能是蝙蝠猫吃​​或吃蝙蝠猫?是否保证在规范的某个地方?

With the above, could the output ever be "bats cats eat" or "eat bats cats"? Is that guaranteed somewhere in the spec?

我以前的回答是不正确的(感谢@shmosel纠正我)。

My previous answer was incorrect (thanks to @shmosel for correcting me).

现在我说,根据规范,它尊重元素的顺序。

Now I state, that according to the specification it respects the order of the elements.

证明非常简单。 规范声称缩减函数必须 关联

Proof is very simple. The specification claims that a reduction function has to be associative.

然而, 关联性 如果不保留订单,那么它自身没有任何意义。根据关联属性的数学定义



行中包含两个或多个匹配项的表达式中,相同的关联运算符,执行
的操作顺序无关紧要只要操作数的顺序是
未更改

换句话说,关联属性并不意味着:

In other words, associative property doesn't imply that:

(a + b) + c = (a + c) + b

它只允许对应用操作的顺序进行任意排列。

It only allows arbitrary permutation of the order in which operations are applied.