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?
Yes, but the function itself is executed without any particular order.
List(1,2,3).par foreach print // could print out 213
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