The map() method is utilized to apply the stated function to all the elements of the list.
Method Definition: def map[B](f: (A) => B): List[B]
Return Type: It returns a new list after applying the stated function to all the elements of the list.
Example #1:
object GfG
{
def main(args : Array[String])
{
val m 1 = List( 2 , 3 , 5 , 7 , 8 )
val result = m 1 .map(x => x* 3 )
println(result)
}
}
|
Output:
List(6, 9, 15, 21, 24)
Example #2:
object GfG
{
def main(args : Array[String])
{
val m 1 = List( 2 , 3 , 5 , 7 , 8 )
val result = m 1 .map(x => x*x)
println(result)
}
}
|
Output:
List(4, 9, 25, 49, 64)