Will Scala's parallel collections guarantee ordering?

99封情书 提交于 2019-12-05 12:24:44

问题


If I have this:

val a = Array(...)

and I write

a.par.map(e => someFunc(e))

Will the resulting collection be in the same order as the non-parallel collection?


回答1:


Yes, but the function itself is executed without any particular order.

List(1,2,3).par foreach print // could print out 213



回答2:


The parallel collections maintain all of the contracts of their non-parallel equivalents.

On collections in which a map operation preserves order, such as List, order will be preserved by the parallel map as well. On collections in which map does not preserve order, such as Set, order will not be preserved in the parallel version.

With unordered collections, there is no guarantee that the result of a parallel operation will even have the same traversal order as its non-parallel equivalent.



来源:https://stackoverflow.com/questions/6651444/will-scalas-parallel-collections-guarantee-ordering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!