Back
Close

Introduction to Scala Part2 : Collections

Bubu
5,791 views

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

Map

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.

Map

> 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
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