import scala.collection.immutable.HashMap //immutable不可变map import scala.collection.mutable.Map //immutable可变map //1 var m1 = Map("a" -> 1, "b" -> 2) m1 += ("c" -> 3, "d" -> 4) println(m1.get("d")) m1 -= "d" for ((k, v) <- m1) print("k:" + k + " v:" + v) //遍历k和v for (k <- m1.keySet) print("key:" + k) //遍历k for (v <- m1.values) print("value:" + v) //遍历v //yield关键字用于在for循环,每一步记录下当前的状态并保存,最后输出新的集合 var new_map = for ((k, v) <- m1) yield (v, k) // 反转key和value生成新的map for ((k, v) <- new_map) print(" 新map k:" + k + " v:" + v) //遍历新map println(m1("c")) println(m1.get("d")) //val hi1 = m1.getOrElse("c") //println(hi1) //2 var hm1 = HashMap("a" -> 1, "b" -> 2) var hm2 = hm1 + ("c" -> 3, "d" -> 4) var i = hm2.get("d") println(i) var ary1 = Array(1, 2, 3).map((x0: Int) => 3 * x0) for (item <- 0 until ary1.length) { println(ary1(item)) } //函数仅仅包含一个参数,并且在右侧x0如果只使用1次则可以使用“_”代表x0 var ary2 = Array(3, 5, 7).map(_ * 3) for (item <- 0 until ary2.length) { println(ary1(item)) }