Conversion from scala parallel collection to regular collection

╄→尐↘猪︶ㄣ 提交于 2019-12-05 08:55:42

问题


I'm trying to convert back from a parallel collection to a regular map. According to the api, if I call toMap on any appropriately defined parallel collection, it's supposed to return a standard Map, but it's returning ParMap over a flattened collection of iterables.

I have a

val task: Stream[Future[Iterable[Tuple2[String, String]]]]

And from which I get:

val res: ParSeq[Iterable[Tuple2[String, String]]] = tasks.par.map(f => f.apply())

Finally:

val finalresult = res.flatten.toMap

Unfortunately, the type of finalresult is ParMap[String, String].

On the other hand, if I call it like:

tasks.par.map(f => f.apply()).reduce(_++_).toMap

then the return type is Map[String, String].

Can someone tell me why this is? And (out of curiosity) how I can force convert a ParMap to a Map when scala won't let me?


回答1:


As you go explicitly from sequential to parallel collection via .par, you go back to sequential via .seq. Since sets and maps have parallel implementations, toMap and toSet calls leave the collection in the current domain.

The example of reduce works because it, well, reduces the collection (the outer ParSeq disappears, leaving you with the inner (sequential) Iterable[Tuple2[...]]).



来源:https://stackoverflow.com/questions/12023045/conversion-from-scala-parallel-collection-to-regular-collection

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