Map相关方法(Common methods for immutable and mutable maps)
1. 不可变Map相关方法(Methods for immutable maps)
m - k:返回新的Map,内容为m去掉key=k的元素
scala> val m = Map(1->"a",2->"b",3->"c")
m: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c)
scala> m - 1
res43: scala.collection.immutable.Map[Int,String] = Map(2 -> b, 3 -> c)
scala> m
res44: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c)
m - (x,y,z):返回新的Map,内容为m删除key=x,y,z的元素
scala> val m = Map(1->"a",2->"b",3->"c")
m: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c)
scala> m - (1,2)
res47: scala.collection.mutable.Map[Int,String] = Map(3 -> c)
m -- c:返回新的Map,内容为m删除(key=集合c中元素)的健值对
scala> val m = Map(1->"a",2->"b",3->"c")
m: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c)
scala> m -- Vector(1,2)
res0: scala.collection.immutable.Map[Int,String] = Map(3 -> c)
scala> m -- List(1,2)
res1: scala.collection.immutable.Map[Int,String] = Map(3 -> c)
2. 可变Map的相关方法(Methods for mutable maps)
mm += (k -> v):对mm追加键值对(k -> v)
scala> val mm = collection.mutable.Map(1->"a",2->"b",3->"c")
mm: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a, 3 -> c)
scala> mm += (4->"d")
res5: mm.type = Map(2 -> b, 4 -> d, 1 -> a, 3 -> c)
mm += (k -> v, k -> v):同时对mm追加多组键值对
scala> val mm = collection.mutable.Map(1->"a",2->"b",3->"c")
mm: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a, 3 -> c)
scala> mm += (4->"d",5->"e")
res6: mm.type = Map(2 -> b, 5 -> e, 4 -> d, 1 -> a, 3 -> c)
mm ++= c:将集合c中键值对全部追加到mm中
scala> val mm = collection.mutable.Map(1->"a")
mm: scala.collection.mutable.Map[Int,String] = Map(1 -> a)
scala> mm ++= Vector(2->"b",3->"c")
res8: mm.type = Map(2 -> b, 1 -> a, 3 -> c)
scala> mm ++= List(4->"d",5->"e")
res9: mm.type = Map(2 -> b, 5 -> e, 4 -> d, 1 -> a, 3 -> c)
mm -= k:删除mm中key=k的键值对
scala> val mm = collection.mutable.Map(1->"a",2->"b",3->"c")
mm: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a, 3 -> c)
scala> mm - 3
res12: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a)
mm -= (k1,k2,k3):删除mm中key=(k1,k2,k3)的所有键值对
scala> val mm = collection.mutable.Map(1->"a",2->"b",3->"c")
mm: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a, 3 -> c)
scala> mm -= (1,3)
res13: mm.type = Map(2 -> b)
mm --= c:删除mm中所有key=c中元素的键值对
scala> val mm = collection.mutable.Map(1->"a",2->"b",3->"c")
mm: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a, 3 -> c)
scala> mm --= List(1,2)
res14: mm.type = Map(3 -> c)
3. Map通用方法(Methods for both mutable and immutable maps)
m(k):返回m中key=k对应的值
scala> val mm = collection.mutable.Map(1->"a",2->"b",3->"c")
mm: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a, 3 -> c)
scala> mm(2)
res15: String = b
m filter p:返回m中符合条件p的键值对
scala> val mm = collection.mutable.Map(1->10,2->20,3->30)
mm: scala.collection.mutable.Map[Int,Int] = Map(2 -> 20, 1 -> 10, 3 -> 30)
scala> mm.filter(_ == (1->10))
res41: scala.collection.mutable.Map[Int,Int] = Map(1 -> 10)
m filterKeys p:返回m中key符合条件p的键值对
scala> val mm = collection.mutable.Map(1->10,2->20,3->30)
mm: scala.collection.mutable.Map[Int,Int] = Map(2 -> 20, 1 -> 10, 3 -> 30)
scala> mm.filterKeys(_ > 1)
res42: scala.collection.Map[Int,Int] = Map(2 -> 20, 3 -> 30)
m get(k):以Some(value)的形式返回m中key=k的值,如果为空则返回None
scala> val mm = collection.mutable.Map(1->10,2->20,3->30)
mm: scala.collection.mutable.Map[Int,Int] = Map(2 -> 20, 1 -> 10, 3 -> 30)
scala> mm.get(1)
res43: Option[Int] = Some(10)
scala> mm.get(4)
res44: Option[Int] = None
m getOrElse(k,d):返回m中key=k的值,如果没有则返回d
scala> val mm = collection.mutable.Map(1->10,2->20,3->30)
mm: scala.collection.mutable.Map[Int,Int] = Map(2 -> 20, 1 -> 10, 3 -> 30)
scala> mm.getOrElse(4,50)
res48: Int = 50
scala> mm.getOrElse(2,50)
res49: Int = 20
m isDefinedAt(k):如果m中有key=k的元素那么返回true,否则返回false
scala> val mm = collection.mutable.Map(1->10,2->20,3->30)
mm: scala.collection.mutable.Map[Int,Int] = Map(2 -> 20, 1 -> 10, 3 -> 30)
scala> mm.isDefinedAt(2)
res50: Boolean = true
scala> mm.isDefinedAt(5)
res51: Boolean = false
m keys:返回m的所有key生成的Set集合
scala> val mm = collection.mutable.Map(1->10,2->20,3->30)
mm: scala.collection.mutable.Map[Int,Int] = Map(2 -> 20, 1 -> 10, 3 -> 30)
scala> mm.keys
res52: Iterable[Int] = Set(2, 1, 3)
m keysIterator:返回m的所有key做为Iterator
scala> val m = Map(1->"a",2->"b",3->"c")
m: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c)
scala> m.keysIterator
res7: Iterator[Int] = non-empty iterator
m keySet:返回m的key做为一个Set
scala> val m = Map(1->"a",2->"b",3->"c")
m: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c)
scala> m.keys
res9: Iterable[Int] = Set(1, 2, 3)
scala> m.keySet
res10: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
m.mapValues:返回一个新的map,内容为使用f函数对原map的value进行操作
scala> val m = Map(1->"a",2->"b",3->"c")
m: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c)
scala> m.mapValues(_ + "d")
res14: scala.collection.immutable.Map[Int,String] = Map(1 -> ad, 2 -> bd, 3 -> cd)
m.values:以Iterable形式返回m的所有值
scala> val m = Map(1->"a",2->"b",3->"c")
m: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c)
scala> m.values
res16: Iterable[String] = MapLike(a, b, c)
m.valueIterator:返回m所有值的Iterator
scala> m.valuesIterator
res17: Iterator[String] = non-empty iterator