我如何在 Scala 中生成一个 immutable.Map ?

发布于 2024-08-31 05:05:22 字数 177 浏览 7 评论 0原文

我已经尝试过这个,但它不起作用:

val map:Map[String,String] = for {
    tuple2 <- someList
  } yield tuple2._1 -> tuple2._2

我还能如何将 Tuple2 列表转换为映射?

I have tried this but it does not work:

val map:Map[String,String] = for {
    tuple2 <- someList
  } yield tuple2._1 -> tuple2._2

How else would I convert a List of Tuple2s into a Map?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟─花易冷 2024-09-07 05:05:22

它再简单不过了:

Map(listOf2Tuples: _*)

使用 Map 伴随对象中的apply 方法。

It couldn't be simpler:

Map(listOf2Tuples: _*)

using the apply method in Map companion object.

蘑菇王子 2024-09-07 05:05:22

我的第一次尝试是这样的:

scala> val country2capitalList = List("England" -> "London", "Germany" -> "Berlin")
country2capitalList: List[(java.lang.String, java.lang.String)] = List((England,London), (Germany,Berlin))

scala> val country2capitalMap = country2capital.groupBy(e => e._1).map(e => (e._1, e._2(0)._2))
country2capitalMap: scala.collection.Map[java.lang.String,java.lang.String] = Map(England -> London, Germany -> Berlin)

但这是最好的解决方案:

scala> val betterConversion = Map(country2capitalList:_*)
betterConversion: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(England -> London, Germany -> Berlin)

需要 :_* 来给编译器提示使用列表作为 varargs 参数。否则它会给你:

scala> Map(country2capitalList)
<console>:6: error: type mismatch;
 found   : List[(java.lang.String, java.lang.String)]
 required: (?, ?)
       Map(country2capitalList)
           ^

从 Scala 2.8 开始,你可以使用 toMap

scala> val someList = List((1, "one"), (2, "two"))   
someList: List[(Int, java.lang.String)] = List((1,one), (2,two))

scala> someList.toMap
res0: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,two))

My First try is this:

scala> val country2capitalList = List("England" -> "London", "Germany" -> "Berlin")
country2capitalList: List[(java.lang.String, java.lang.String)] = List((England,London), (Germany,Berlin))

scala> val country2capitalMap = country2capital.groupBy(e => e._1).map(e => (e._1, e._2(0)._2))
country2capitalMap: scala.collection.Map[java.lang.String,java.lang.String] = Map(England -> London, Germany -> Berlin)

But here is the best solution:

scala> val betterConversion = Map(country2capitalList:_*)
betterConversion: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(England -> London, Germany -> Berlin)

The :_* is needed to give the compiler a hint to use the list as a varargs argument. Otherwise it will give you:

scala> Map(country2capitalList)
<console>:6: error: type mismatch;
 found   : List[(java.lang.String, java.lang.String)]
 required: (?, ?)
       Map(country2capitalList)
           ^

From Scala 2.8 on you can use toMap:

scala> val someList = List((1, "one"), (2, "two"))   
someList: List[(Int, java.lang.String)] = List((1,one), (2,two))

scala> someList.toMap
res0: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,two))
静若繁花 2024-09-07 05:05:22

在 2.8 中,您可以使用 toMap 方法:

scala> val someList = List((1, "one"), (2, "two"))   
someList: List[(Int, java.lang.String)] = List((1,one), (2,two))

scala> someList.toMap
res0: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,two))

这适用于任何对的集合。请注意,文档对其重复策略有这样的说法:

重复的键将被覆盖
后面的键:如果这是无序的
集合,哪个键在
结果地图未定义。

In 2.8, you can use the toMap method:

scala> val someList = List((1, "one"), (2, "two"))   
someList: List[(Int, java.lang.String)] = List((1,one), (2,two))

scala> someList.toMap
res0: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,two))

This will work for any collection of pairs. Note that the documentation has this to say about its duplicate policy:

Duplicate keys will be overwritten by
later keys: if this is an unordered
collection, which key is in the
resulting map is undefined.

深巷少女 2024-09-07 05:05:22

在 scala 2.8 中:

scala> import scala.collection.breakOut
import scala.collection.breakOut

scala> val ls = List("a","bb","ccc")
ls: List[java.lang.String] = List(a, bb, ccc)

scala> val map: Map[String,Int] = ls.map{ s => (s,s.length) }(breakOut)
map: Map[String,Int] = Map((a,1), (bb,2), (ccc,3))

scala> val map2: Map[String,Int] = ls.map{ s => (s,s.length) }.toMap
map2: Map[String,Int] = Map((a,1), (bb,2), (ccc,3))

scala>

In scala 2.8:

scala> import scala.collection.breakOut
import scala.collection.breakOut

scala> val ls = List("a","bb","ccc")
ls: List[java.lang.String] = List(a, bb, ccc)

scala> val map: Map[String,Int] = ls.map{ s => (s,s.length) }(breakOut)
map: Map[String,Int] = Map((a,1), (bb,2), (ccc,3))

scala> val map2: Map[String,Int] = ls.map{ s => (s,s.length) }.toMap
map2: Map[String,Int] = Map((a,1), (bb,2), (ccc,3))

scala>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文