从Scala并行收集转换为常规收集 [英] Conversion from scala parallel collection to regular collection

查看:64
本文介绍了从Scala并行收集转换为常规收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从并行集合转换回常规地图.根据该api,如果我在任何适当定义的并行集合上调用toMap,则应该返回一个标准Map,但它将在一个扁平化的Iterable集合上返回ParMap.

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.

我有一个

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

从中我得到:

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

最后:

val finalresult = res.flatten.toMap

不幸的是,finalresult的类型是ParMap[String, String].

另一方面,如果我这样称呼它:

On the other hand, if I call it like:

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

则返回类型为Map[String, String].

有人可以告诉我为什么吗? (出于好奇)当scala不允许我将我如何强制将ParMap转换为Map?

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?

推荐答案

当您通过.par从顺序收集显式转到并行收集时,您将通过.seq回到顺序处理.由于集合和映射具有并行实现,因此toMaptoSet调用将集合保留在当前域中.

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.

reduce的示例之所以有效,是因为它可以减少集合(外部的ParSeq消失,而剩下内部的(顺序)Iterable[Tuple2[...]]).

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

这篇关于从Scala并行收集转换为常规收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆