Introduction to Scala Part2 : Collections

Bubu
5,757 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

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
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content