【发布时间】:2018-06-20 02:23:52
【问题描述】:
Stream.of(a, b, c).parallel().map(Object::toString).iterator();
返回的迭代器是否保证按顺序提供值a、b、c?
我知道toArray() 和collect() 保证集合的值顺序正确。另外,我不是在问如何从迭代器中创建流。
【问题讨论】:
-
我知道 toArray() 和 collect() 保证集合具有正确顺序的值。 你在哪里看到的?许多集合甚至没有排序的概念。
-
好的,我在Side-effects 部分找到了这个引用:
IntStream.range(0,5).parallel().map(x -> x*2).toArray()必须产生[0, 2, 4, 6, 8]。奇怪的是文档并不清楚哪些操作尊重遇到顺序,特别是考虑到forEach()、forEachOrdered()的区别。 -
松散相关:我关于 How does Stream.max() handle equality? 的老问题 Holger 在哪里挖出了 Brian Goetz 的以下评论:"If the stream is ordered (such as the streams you get from an array or List), it returns the first element that is maximal in the event of multiple maximal elements; only if the stream is unordered is it allowed to pick an arbitrary element."
-
...它没有提到顺序与并行,但我认为这是确认所有未明确声明它们删除排序或明确不确定的 Stream 操作都需要保持遇到订购。
标签: java java-stream