Map is used to build a new collection or set of elements by traversing a function to all elements of the collection. It can be defined formally as follows:
def map[B](f: (A) ⇒ B): Map[B]
Let's see an example as follows:
scala> // Given a list of integers
scala> // Get a list with all the elements square.
scala> List(2, 4, 5, -6) map ( x=> x * x)
res4: List[Int] = List(4, 16, 25, 36)
While using the collection API in Scala, you often need to select the nth elements of the list or array for example. In the next sub-section, we will explore examples of using take.