valmutableMap=mutableMapOf("Alice"to18,"Alex"to20,)println("Alice age = ${mutableMap["Alice"]}")println("Alex age = ${mutableMap.get("Alex")}")println("Momo age = ${mutableMap.getOrDefault("Momo", 0)}")println("Yoyo age = ${mutableMap.getOrElse("Yoyo") {0}}")
Alice age = 18
Alex age = 20
Momo age = 0
Yoyo age = 0
遍歷
1
2
3
4
5
6
7
8
9
valmutableMap=mutableMapOf("Alice"to18,"Alex"to20,"Momo"to5,"Yoyo"to8)mutableMap.forEach{println("key = ${it.key} , value = ${it.value}")}
key = Alice , value = 18
key = Alex , value = 20
key = Momo , value = 5
key = Yoyo , value = 8
1
2
3
mutableMap.forEach{key,value->println("key = $key , value = $value")}