Back
Close

Introduction to Scala Part2 : Collections

Bubu
5,785 views

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 Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io