Map 的特色是 key / value.有分為 mutable 及 immutable.
scala> val m = Map("a1" -> 1 , "a2" -> 2 , "a3" -> 3)
m: scala.collection.immutable.Map[String,Int] = Map(a1 -> 1, a2 -> 2, a3 -> 3)
看 Map 的 kyes 及 values :
scala> m.keys
res163: Iterable[String] = Set(a1, a2, a3)
scala> m.values
res164: Iterable[Int] = MapLike.DefaultValuesIterable(1, 2, 3)
取得元素 :
scala> m get "a2"
res167: Option[Int] = Some(2)
scala> m("a2")
res165: Int = 2
使用 contains 判斷 key 是否存在 :
scala> m contains "a5"
res168: Boolean = false
scala> m contains "a2"
res169: Boolean = true
getOrElse 取得 key 不存在的話就給參數 2 的值 :
scala> val msg = m getOrElse ("a5" , "nodata")
msg: Any = nodata
新增元素 :
scala> m + ("a4" -> 4)
res172: scala.collection.immutable.Map[String,Int] = Map(a1 -> 1, a2 -> 2, a3 -> 3, a4 -> 4)
scala> m + ("a5" -> 5 , "a6" -> 6)
res174: scala.collection.immutable.Map[String,Int] = Map(a6 -> 6, a5 -> 5, a3 -> 3, a1 -> 1, a2 -> 2)
使用 ++ 將兩個 Map 相加,並將重複的 key 使用新的 map 代入,a1 原本是 3 會變 33 :
scala> val nm = Map("a1" -> 33 , "b1" -> 11 , "b2" -> 22)
nm: scala.collection.immutable.Map[String,Int] = Map(a1 -> 33, b1 -> 11, b2 -> 22)
scala> m ++ nm
res179: scala.collection.immutable.Map[String,Int] = Map(b2 -> 22, b1 -> 11, a3 -> 3, a1 -> 33, a2 -> 2)
移除元素 :
scala> m - "a2"
res183: scala.collection.immutable.Map[String,Int] = Map(a1 -> 1, a3 -> 3)
scala> m - ("a1","a3")
res184: scala.collection.immutable.Map[String,Int] = Map(a2 -> 2)
使用 filterKeys 找到想要的元素 :
scala> m
res196: scala.collection.immutable.Map[String,Int] = Map(a1 -> 1, a2 -> 2, a3 -> 3)
scala> m.filterKeys(_ == "a2")
res197: scala.collection.immutable.Map[String,Int] = Map(a2 -> 2)
需要 import scala.collection.mutable.Map :
scala> import scala.collection.mutable.Map
import scala.collection.mutable.Map
scala> val im = Map("a1" -> 1 , "a2" -> 2 , "a3" -> 3)
im: scala.collection.mutable.Map[String,Int] = Map(a1 -> 1, a3 -> 3, a2 -> 2)
新增元素 :
scala> im += ("a4" -> 4 , "a5" -> 5)
res191: im.type = Map(a1 -> 1, a3 -> 3, a5 -> 5, a2 -> 2, a4 -> 4)
移除元素 :
scala> im -= ("a1","a3")
res192: im.type = Map(a5 -> 5, a2 -> 2, a4 -> 4)
mutable 的 Map 與 immutable 差異就是 immutable 的不可以變動,所以不能使用 += 或 -= 改變原來的 Map :
scala> m -= ("a1","a3")
<console>:20: error: value -= is not a member of scala.collection.immutable.Map[String,Int]
Expression does not convert to assignment because receiver is not assignable.
m -= ("a1","a3")
^
使用 ++= 可將新的 Map 加入原來的 Map :
scala> val nm = Map("c1" -> 11,"c2" -> 22,"c3" -> 33)
nm: scala.collection.mutable.Map[String,Int] = Map(c1 -> 11, c3 -> 33, c2 -> 22)
scala> im ++= nm
res199: im.type = Map(c1 -> 11, c3 -> 33, a5 -> 5, a2 -> 2, c2 -> 22, a4 -> 4)
看 Map 的 kyes 及 values,這裡的 values 跟 immutable Map 的 return type 不太一樣是 HashMap :
scala> im.keys
res194: Iterable[String] = Set(a5, a2, a4)
scala> im.values
res195: Iterable[Int] = HashMap(5, 2, 4)
mutable 的 Map 可以使用 clear 清空,immutable 的不行 :
scala> im.clear
scala> im
res205: scala.collection.mutable.Map[String,Int] = Map()
scala> m.clear
<console>:20: error: value clear is not a member of scala.collection.immutable.Map[String,Int]
m.clear
^