11.6.3 遍历 Map
package com.atguigu.day11.map
import scala.collection.mutable
object MapDemo5 {
def main(args: Array[String]): Unit = {
val map1 =
mutable.Map("a" -> 1, "c" -> 2, "b" -> 3)
//方法1: k v 一起遍历
for ((k, v) <- map1) {
println(k + " = " + v)
}
println("-----------")
// 方法2: 只遍历key
for (k <- map1.keys) {
println("k = " + k)
}
println("-----------")
// 方法3: 只遍历value
for (v <- map1.values) {
println("v = " + v)
}
println("-----------")
// 方法4: 遍历出来tuple2, 然后 _1 _2 来获取k 和v
for (kv <- map1) {
println(kv._1 +" = " + kv._2)
}
}
}