集合常用函数
1 forEach
package com._51doit.day03.demo
/**
* FileName: ForEachDemo
* Author: 多易教育-DOIT
* Date: 2020/11/2 0002
* Description:
* 对集合中的每个元素处理 适用所有的集合类型
* Array
* List
* Iterator
* Set
* Map
* 注意 : 方法是没有返回值 常用于打印测试
*/
object ForEachDemo {
def main(args: Array[String]): Unit = {
val ls = List(1,2,3,4)
val mp = Map[String,Int](("a",1),"b"->2,"c"->3)
// list
ls.foreach(e=>println(e))
ls.foreach(e=>println(e*10))
ls.foreach(println(_))
ls.foreach(println)
ls.foreach(e=>print(e+" "))
// 映射集合 Map
mp.foreach(tp=>println(tp))
mp.foreach(tp=>println(tp._1))
mp.foreach(tp=>println(tp._2))
/* mp.foreach(tp =>{
tp._1
tp._2
})*/
/* ls.foreach(e=>{
val res: Int = e * 100 + 10
println("hello"+res)
res
})*/
}
}
package com._51do