Introduction to Scala Part2 : Collections


Traversable : Conversion

  • Operations:
    • toArray,
    • toList,
    • toIterable,
    • toSeq,
    • toIndexedSeq,
    • toStream,
    • toSet,
    • toMap
  • Goal: turn a Traversable collection into something more specific.

All these conversions return their receiver argument unchanged if the run-time type of the collection already matches the demanded collection type. For instance, applying toList to a list will yield the list itself.

> List(1, 2, 4).toArray
res0: Array[Int] = Array(1, 2, 4)
convert Iterable -> List
convert List -> Iterable
convert Seq -> List
convert List -> Seq
convert Array -> List
convert List of Int -> Array of Int
Create your playground on
This playground was created on, our hands-on, knowledge-sharing platform for developers.
Go to