并行流上的iterator()保证遇到命令? [英] iterator() on parallel stream guarantee encounter order?

查看:111
本文介绍了并行流上的iterator()保证遇到命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stream.of(a, b, c).parallel().map(Object::toString).iterator();

返回的迭代器是否保证按顺序提供值2,3,4?

Is the returned iterator guaranteed to provide the values 2, 3, 4 in that order?

我知道toArray()和collect()保证集合的值正确。另外,我不是问如何从迭代器创建流。

I'm aware toArray() and collect() guarantee collections with values in the correct order. Also, I'm not asking how to make a stream from an iterator.

推荐答案

这是规范中的疏忽。如果流具有已定义的遭遇顺序,则意图是其Iterator按照遭遇顺序生成元素。如果流没有定义的遭遇顺序,Iterator当然会以某些顺序生成元素,但不会定义该顺序。

This is an oversight in the specification. If a stream has a defined encounter order, the intent was that its Iterator produce the elements in encounter order. If the stream has no defined encounter order, the Iterator will of course produce the elements in some order, but that order won't be defined.

我已经提交了 JDK-8194952 错误来跟踪对规范的更改。

I've filed bug JDK-8194952 to track the change to the specification.

看起来其他人已经浏览了足够多的实现,以表明它确实会产生遇到顺序的元素。此外,我们的流测试依赖于此属性。例如, toList 收集器的测试断言列表中的元素的顺序与从流的迭代器中获取的顺序相同。所以你依赖这种行为可能是安全的,即使它尚未正式指定(还)。

It looks like others have crawled through enough of the implementation to show that it will indeed produce the elements in encounter order. In addition, our stream tests rely on this property. For example, the test for the toList collector asserts that the elements in the list are present in the same order as they are obtained from the stream's Iterator. So it's probably safe for you to rely on this behavior, even though it isn't formally specified (yet).

这篇关于并行流上的iterator()保证遇到命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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