defmap[B](f: (A) ⇒ B): Seq[B] [use case] Builds a new collection by applying a function to all elements of this sequence. B the element typeof the returned collection. f the function to apply to each element. returns a new sequence resulting from applying the given function f to each element of this sequence and collecting the results.
Demo2 函数式实现
object MapOperateDemo2 {defmain(args: Array[String]): Unit = {val list = List(1, 2, 3)val list2 = list.map(multiple) println(list2)// 简化写法val list3 = list.map(_ *2) println(list3)val list4 = list.map(n => n *2) println(list4) }defmultiple(n: Int): Int = { println("被调用") //调用三次 n *2 }}