views:

620

answers:

2

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?

+9  A: 

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))
Eastsun
A: 

val map = Map(seq: _*) gave me that "scala feeling" in my stomach again :) Thanks

Vilius Normantas