1 package chapter07
2
3 import scala.collection.mutable
4
5 object Test09_MutableMap {
6 def main(args: Array[String]): Unit = {
7 // 1. 创建map
8 val map1: mutable.Map[String, Int] = mutable.Map("a" -> 13, "b" -> 25, "hello" -> 3)
9 println(map1)
10 println(map1.getClass)
11
12 println("==========================")
13
14 // 2. 添加元素
15 map1.put("c", 5)
16 map1.put("d", 9)
17 println(map1)
18
19 map1 += (("e", 7))
20 println(map1)
21
22 println("====================")
23
24 // 3. 删除元素
25 println(map1("c"))
26 map1.remove("c")
27 println(map1.getOrElse("c", 0))
28
29 map1 -= "d"
30 println(map1)
31
32 println("====================")
33
34 // 4. 修改元素
35 map1.update("c", 5)
36 map1.update("e", 10)
37 println(map1)
38
39 println("====================")
40
41 // 5. 合并两个Map
42 val map2: Map[String, Int] = Map("aaa" -> 11, "b" -> 29, "hello" -> 5)
43 // map1 ++= map2
44 println(map1)
45 println(map2)
46
47 println("---------------------------")
48 val map3: Map[String, Int] = map2 ++ map1
49 println(map1)
50 println(map2)
51 println(map3)
52 }
53 }
Scala mutable.Map可变的Map
最新推荐文章于 2023-04-20 12:06:42 发布