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
A subtrait of collection. Seq which represents sequences that are guaranteed immutable.
Seq is immutable — Once a Seq is created, it cannot be changed, appended to, rearranged or otherwise modified. Instead, any mutative method called on a Seq will return a new Seq .
Iterable: A base trait for iterable collections. This is a base trait for all Scala collections that define an iterator method to step through one-by-one the collection's elements.
Scala has a rich set of collection library. Collections are containers of things. Those containers can be sequenced, linear sets of items like List, Tuple, Option, Map, etc. The collections may have an arbitrary number of elements or be bounded to zero or one element (e.g., Option).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With