Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Scala List map() method with example

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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:




// Scala program of map()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
        // Creating a list
        val m1 = List(2, 3, 5, 7, 8)
          
        // Applying map method
        val result = m1.map(x => x*3)
          
        // Displays output
        println(result)
      
    }


Output:

List(6, 9, 15, 21, 24)

Example #2:




// Scala program of map()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
        // Creating a list
        val m1 = List(2, 3, 5, 7, 8)
          
        // Applying map method
        val result = m1.map(x => x*x)
          
        // Displays output
        println(result)
      
    }


Output:

List(4, 9, 25, 49, 64)

My Personal Notes arrow_drop_up
Last Updated : 13 Aug, 2019
Like Article
Save Article
Similar Reads