Introduction to Scala Part2 : Collections


Open Source Your Knowledge, Become a Contributor

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

Create Content

Traversable: Map operations

  • Operations:
    • map,
    • flatMap,
    • collect
    • collectFirst
  • Goal: produce a new collection by applying some function to collection elements.

collect and collectFirst methods will be see in course C#03


def map[B](f: (A) ⇒ B): Traversable[B]

Map is a HOF that builds a new collection by applying a function to all elements of this collection.


> List("John Snow", "Aria Stark", "Tyrion Lannister").flatMap(x => x.split(" "))
res0: List[String] = List(John, Snow, Aria, Stark, Tyrion, Lannister)
Split all the sentences in Lists of words
count the words in l
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content