Scala convert Iterable or collection.Seq to collection.immutable.Seq

Richard Todd picture Richard Todd · Jun 5, 2013 · Viewed 37k times · Source

It appears the toSeq method in Scala collections returns a scala.collection.Seq, I could also return a Traversable or Iterable but need to convert this to a scala.collection.immutable.Seq.

Is there an easy way to do this?

Thanks Richard

Answer

axel22 picture axel22 · Jun 5, 2013

Use the to method to convert between arbitrary collection types in Scala 2.10:

scala> Array(1, 2, 3).toSeq
res0: Seq[Int] = WrappedArray(1, 2, 3)

scala> Array(1, 2, 3).to[collection.immutable.Seq]
res1: scala.collection.immutable.Seq[Int] = Vector(1, 2, 3)