Will Scala's parallel collections guarantee or

2019-04-03 08:16发布

问题:

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.