📜  Scala List map() 方法与示例

📅  最后修改于: 2022-05-13 01:55:10.589000             🧑  作者: Mango

Scala List map() 方法与示例

map()方法用于将所述函数应用于列表的所有元素。

示例 #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)
      
    }
} 
输出:
List(6, 9, 15, 21, 24)

示例 #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)
      
    }
} 
输出:
List(4, 9, 25, 49, 64)