如何将Map [A​​,Future [B]]转换为Future [Map [A​​,B]]?

编程入门 行业动态 更新时间:2024-10-26 16:35:10
本文介绍了如何将Map [A​​,Future [B]]转换为Future [Map [A​​,B]]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在使用Scala Akka库,遇到了一些问题。如标题所示,我需要将 Map [A​​,Future [B]] 转换为 Future [Map [A​​,B]] 。我知道可以将 Future.sequence 用于列表之类的Iterable,但这在这种情况下不起作用。

I've been working with the Scala Akka library and have come across a bit of a problem. As the title says, I need to convert Map[A, Future[B]] to Future[Map[A,B]]. I know that one can use Future.sequence for Iterables like Lists, but that doesn't work in this case.

我想知道:Scala中有没有一种干净的方法来进行这种转换?

I was wondering: is there a clean way in Scala to make this conversion?

推荐答案

看看这是否对您有用:

See if this works for you:

val map = Map("a" -> future{1}, "b" -> future{2}, "c" -> future{3}) val fut = Future.sequence(map.map(entry => entry._2.map(i => (entry._1, i)))).map(_.toMap)

这个想法是将地图映射到可迭代获取地图键的元组和与该键相关的未来结果。从那里,您可以顺序可迭代的 ,然后获得总计的未来,将其映射并通过 toMap将 Tuples 的 Iterable 转换为地图。

The idea is to map the map to an Iterable for a Tuple of the key of the map and the result of the future tied to that key. From there you can sequence that Iterable and then once you have the aggregate Future, map it and convert that Iterable of Tuples to a map via toMap.

现在,这种方法的另一种选择是尝试执行与 sequence 函数相似的操作,进行了一些调整。您可以像这样编写 sequenceMap 函数:

Now, an alternative to this approach is to try and do something similar to what the sequence function is doing, with a couple of tweaks. You could write a sequenceMap function like so:

def sequenceMap[A, B](in: Map[B, Future[A]])(implicit executor: ExecutionContext): Future[Map[B, A]] = { val mb = new MapBuilder[B,A, Map[B,A]](Map()) in.foldLeft(Promise.successful(mb).future) { (fr, fa) => for (r <- fr; a <- fa._2.asInstanceOf[Future[A]]) yield (r += ((fa._1, a))) } map (_.result) }

然后在这样的示例中使用它:

And then use it in an example like this:

val map = Map("a" -> future{1}, "b" -> future{2}, "c" -> future{3}) val fut = sequenceMap(map) fut onComplete{ case Success(m) => println(m) case Failure(ex) => ex.printStackTrace() }

这可能比第一个示例效率更高,因为它创建的中间集合较少,对 ExecutionContext 的命中较少。

This might be slightly more efficient than the first example as it creates less intermediate collections and has less hits to the ExecutionContext.

更多推荐

如何将Map [A​​,Future [B]]转换为Future [Map [A​​,B]]?

本文发布于:2023-11-25 03:12:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628086.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   如何将   Map   Future

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!