如何从 seq 创建不可变的映射/集?

发布于 2024-08-17 01:34:34 字数 261 浏览 4 评论 0原文

我尝试从 Seq 构造不可变的集合/映射。我目前正在做以下事情:

val input: Seq[(String, Object)] = //.....
Map[String, Object]() ++ input

对于集合

val input: Seq[String] = //.....
Set[String]() ++ input

这似乎有点令人费解,有更好的方法吗?

I am try to construct immutable Sets/Maps from a Seq. I am currently doing the following:

val input: Seq[(String, Object)] = //.....
Map[String, Object]() ++ input

and for sets

val input: Seq[String] = //.....
Set[String]() ++ input

Which seems a little convoluted, is there a better way?

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

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

发布评论

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

评论(2

<逆流佳人身旁 2024-08-24 01:34:34

在Scala 2.8中:

Welcome to Scala version 2.8.0.r20327-b20091230020149 (Java HotSpot(TM) Client VM, Java 1.6.
Type in expressions to have them evaluated.
Type :help for more information.

scala> val seq: Seq[(String,Object)] = ("a","A")::("b","B")::Nil
seq: Seq[(String, java.lang.Object)] = List((a,A), (b,B))

scala> val map = Map(seq: _*)
map: scala.collection.immutable.Map[String,java.lang.Object] = Map(a -> A, b -> B)

scala> val set = Set(seq: _*)
set: scala.collection.immutable.Set[(String, java.lang.Object)] = Set((a,A), (b,B))

scala>

Edit 2010.1.12

我发现有一种更简单的方法来创建集合。

scala> val seq: Seq[(String,Object)] = ("a","A")::("b","B")::Nil
seq: Seq[(String, java.lang.Object)] = List((a,A), (b,B))

scala> val set = seq.toSet
set: scala.collection.immutable.Set[(String, java.lang.Object)] = Set((a,A), (b,B))

In Scala 2.8:

Welcome to Scala version 2.8.0.r20327-b20091230020149 (Java HotSpot(TM) Client VM, Java 1.6.
Type in expressions to have them evaluated.
Type :help for more information.

scala> val seq: Seq[(String,Object)] = ("a","A")::("b","B")::Nil
seq: Seq[(String, java.lang.Object)] = List((a,A), (b,B))

scala> val map = Map(seq: _*)
map: scala.collection.immutable.Map[String,java.lang.Object] = Map(a -> A, b -> B)

scala> val set = Set(seq: _*)
set: scala.collection.immutable.Set[(String, java.lang.Object)] = Set((a,A), (b,B))

scala>

Edit 2010.1.12

I find that there is a more simple way to create set.

scala> val seq: Seq[(String,Object)] = ("a","A")::("b","B")::Nil
seq: Seq[(String, java.lang.Object)] = List((a,A), (b,B))

scala> val set = seq.toSet
set: scala.collection.immutable.Set[(String, java.lang.Object)] = Set((a,A), (b,B))
桃扇骨 2024-08-24 01:34:34

要将 Seq 转换为 Map,只需在 Seq 上调用 toMap 即可。请注意,Seq 的元素必须是 Tuple2 即。 (X,Y)(X->Y)

scala> val seq: Seq[(String,String)] = ("A","a")::("B","b")::("C","c")::Nil
seq: Seq[(java.lang.String, java.lang.String)] = List((A,a), (B,b), (C,c))

scala> seq.toMap
res0: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map((A,a), (B,b), (C,c))

要将 Seq 转换为 Set,只需在 Seq 上调用 toSet

scala> val seq: Seq[String] = "a"::"b"::"c"::Nil
seq: Seq[java.lang.String] = List(a, b, c)

scala> seq.toSet
res1: scala.collection.immutable.Set[java.lang.String] = Set(a, b, c)

To convert a Seq to a Map, simply call toMap on the Seq. Note that the elements of the Seq must be Tuple2 ie. (X,Y) or (X->Y)

scala> val seq: Seq[(String,String)] = ("A","a")::("B","b")::("C","c")::Nil
seq: Seq[(java.lang.String, java.lang.String)] = List((A,a), (B,b), (C,c))

scala> seq.toMap
res0: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map((A,a), (B,b), (C,c))

To convert a Seq to a Set, simply call toSet on the Seq.

scala> val seq: Seq[String] = "a"::"b"::"c"::Nil
seq: Seq[java.lang.String] = List(a, b, c)

scala> seq.toSet
res1: scala.collection.immutable.Set[java.lang.String] = Set(a, b, c)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文