📅  最后修改于: 2021-01-05 07:49:57             🧑  作者: Mango
Kotlin MutableMap是集合框架的接口,该集合以键和值对的形式保存对象。 MutableMap接口的值通过使用其对应的键来检索。键和值可以是不同的对,例如
要使用MutableMap接口,我们需要使用其名为mutableMapOf()或mutableMapOf
interface MutableMap : Map (source)
Properties | Description |
---|---|
abstract val entries: MutableSet |
This returns a MutableSet of all its key and value pairs in the map. |
abstract val keys: MutableSet |
This returns all the keys of MutableSet in this map. |
abstract val values: MutableCollection |
This returns all the values of MutableCollection in the current map. This collection may contain duplicate values. |
Function | Description |
---|---|
abstract fun put(key: K, value: V): V? | It adds the given value with the specified key in the map. |
abstract fun putAll(from: Map |
This updates the current map with key/value pairs from the mentioned map. |
abstract fun remove(key: K): V? | It removes the specified key with its corresponding value from the map. |
open fun remove(key: K, value: V): Boolean | It removes the key and value entities from the map only if it exist in the map. |
abstract fun clear() | This function is used to removes all the elements from the map. |
operator fun |
It checks the given key in the map. |
abstract fun containsKey(key: K): Boolean | It returns the true if map contains the specified key. |
fun |
It returns the true if map contains the specified key. |
abstract fun containsValue(value: V): Boolean | It returns true if the map maps one or more keys for the given value. |
fun |
It returns true if the map maps one or more keys for the given value. |
fun |
It returns the total number of entities of the map |
operator fun |
It returns the value corresponding to mention key, or null if no such key found in the map. |
fun key: K, defaultValue: V ): V |
It returns the value with corresponding mention key, or it returns default value if no such mapping for the key in the map. |
fun key: K, defaultValue: () -> V ): V |
It returns the value for the mention key in the map, or it returns the default value function if no such entry found for the given key. |
fun |
It returns the value corresponding to given key, or it throws an exception if no key found in the map. |
让我们创建一个示例,使用mutablemapOf()函数创建一个MutableMap并遍历它。在此示例中,我们以不同的方式创建了MutableMap的三种不同类型(MutableMap
fun main(args: Array) {
val mutableMap1: MutableMap = mutableMapOf(1 to "Ashu", 4 to "Rohan", 2 to "Ajeet", 3 to "Vijay")
val mutableMap2: MutableMap = mutableMapOf()
mutableMap2.put("name", "Ashu")
mutableMap2.put("city", "Delhi")
mutableMap2.put("department", "Development")
mutableMap2.put("hobby", "Playing")
val mutableMap3: MutableMap = mutableMapOf(1 to "Ashu", "name" to "Rohsan", 2 to 200)
println(".....traverse mutableMap1........")
for (key in mutableMap1.keys) {
println("Key = ${key}, Value = ${mutableMap1[key]}")
}
println("......traverse mutableMap2.......")
for (key in mutableMap2.keys) {
println("Key = "+key +", "+"Value = "+mutableMap2[key])
}
println("......traverse mutableMap3......")
for (key in mutableMap3.keys) {
println("Key = ${key}, Value = ${mutableMap3[key]}")
}
}
输出:
.....traverse mutableMap1........
Key = 1, Value = Ashu
Key = 4, Value = Rohan
Key = 2, Value = Ajeet
Key = 3, Value = Vijay
......traverse mutableMap2.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
......traverse mutableMap3......
Key = 1, Value = Ashu
Key = name, Value = Rohsan
Key = 2, Value = 200
函数put()和putAll()用于在MutableMap中添加元素。 put()函数添加单个元素,而putAll()函数在MutableMap中添加集合类型元素。例如:
fun main(args: Array) {
val mutableMap: MutableMap = mutableMapOf()
mutableMap.put("name", "Ashu")
mutableMap.put("city", "Delhi")
val hashMap: HashMap = hashMapOf()
hashMap.put("department", "Development")
hashMap.put("hobby", "Playing")
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = "+key +", "+"Value = "+mutableMap[key])
}
mutableMap.putAll(hashMap)
println("......traverse mutableMap after mutableMap.putAll(hashMap).......")
for (key in mutableMap.keys) {
println("Key = "+key +", "+"Value = "+mutableMap[key])
}
}
输出:
......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
......traverse mutableMap after mutableMap.putAll(hashMap).......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
containsKey()函数用于检查指定的键是否在MutableMap中存在。如果包含指定的键,则返回true,否则返回false。例如:
fun main(args: Array) {
val mutableMap: MutableMap = mutableMapOf()
mutableMap.put("name", "Ashu")
mutableMap.put("city", "Delhi")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "Playing")
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = "+key +", "+"Value = "+mutableMap[key])
}
println("......mutableMap.containsKey(\"city\").......")
println(mutableMap.containsKey("city"))
}
输出:
......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
......mutableMap.containsKey("city").......
true
containsValue()函数用于检查指定的值是否存在于MutableMap中。如果映射映射给定值的一个或多个键,则此函数返回true,否则返回false。例如:
fun main(args: Array) {
val mutableMap: MutableMap = mutableMapOf()
mutableMap.put("name", "Ashu")
mutableMap.put("city", "Delhi")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "Playing")
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = "+key +", "+"Value = "+mutableMap[key])
}
println(".......mutableMap.containsValue(\"Delhi\")......")
println(mutableMap.containsValue("Delhi"))
println(".......mutableMap.containsValue(\"Mumbai\")......")
println(mutableMap.containsValue("Mumbai"))
}
输出:
......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
.......mutableMap.containsValue("Delhi")......
true
.......mutableMap.containsValue("Mumbai")......
false
contains()函数用于检查MutableMap中是否存在指定的键值。如果MutableMap中存在指定的键或值,则它将返回true,否则将返回false。例如:
fun main(args: Array) {
val mutableMap: MutableMap = mutableMapOf()
mutableMap.put("name", "Ashu")
mutableMap.put("city", "Delhi")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "Playing")
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = "+key +", "+"Value = "+mutableMap[key])
}
println("......mutableMap.contains(\"city\").......")
println(mutableMap.contains("city"))
}
输出:
......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
......mutableMap.contains("city").......
true
get(key)函数用于在MutableMap中检索指定键的对应值。如果MutableMap中不存在此类键,则返回null。例如:
fun main(args: Array) {
val mutableMap: MutableMap = mutableMapOf()
mutableMap.put("name", "Ashu")
mutableMap.put("city", "Delhi")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "Playing")
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = "+key +", "+"Value = "+mutableMap[key])
}
println(".......mutableMap.get(\"department\")......")
println(mutableMap.get("department"))
}
输出:
......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
.......mutableMap.get("department")......
Development
用于返回MutableMap的指定键的对应值的getValue()函数,或者如果在地图中找不到键,则抛出异常。例如:
fun main(args: Array) {
val mutableMap: MutableMap = mutableMapOf()
mutableMap.put("name", "Ashu")
mutableMap.put("city", "Delhi")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "Playing")
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = ${key}, Value = ${mutableMap[key]}")
}
println(".......mutableMap.getValue(\"department\")......")
println(mutableMap.getValue("department"))
}
输出:
......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
.......mutableMap.getValue("department")......
Development
getOrDefault()函数返回MutableMap的指定键的对应值。如果MutableMap中不存在此类键,则它将返回默认提及值。例如:
fun main(args: Array) {
val mutableMap: MutableMap = mutableMapOf()
mutableMap.put("name", "Ashu")
mutableMap.put("city", "Delhi")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "Playing")
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = ${key}, Value = ${mutableMap[key]}")
}
println(".......mutableMap.getOrDefault(\"name\", \"Default Value\")......")
println(mutableMap.getOrDefault("name", "default value"))
}
输出:
......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
.......mutableMap.getOrDefault("name", "Default Value")......
Ashu
count()函数用于返回MutableMap中存在的元素总数。例如:
fun main(args: Array) {
val mutableMap: MutableMap = mutableMapOf()
mutableMap.put("name", "Ashu")
mutableMap.put("city", "Delhi")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "Playing")
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = ${key}, Value = ${mutableMap[key]}")
}
println(".....mutableMap.count()........")
println(mutableMap.count())
}
输出:
......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
.....mutableMap.count()........
4
remove(key)函数用于删除与其提及键相对应的值。而remove(key,value)函数删除包含键和值的元素。如果remove(key,value)函数将指定的键及其值一起移除,则返回true,否则返回false。例如:
fun main(args: Array) {
val mutableMap: MutableMap = mutableMapOf()
mutableMap.put("name", "Ashu")
mutableMap.put("city", "Delhi")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "Playing")
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = ${key}, Value = ${mutableMap[key]}")
}
println("......mutableMap.remove(\"city\").......")
println(mutableMap.remove("city"))
println(".......mutableMap.remove(\"hobby\",\"Playing\")......")
println(mutableMap.remove("hobby","Playing"))
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = ${key}, Value = ${mutableMap[key]}")
}
}
输出:
......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
......mutableMap.remove("city").......
Delhi
.......mutableMap.remove("hobby","Playing")......
true
......traverse mutableMap after remove.......
Key = name, Value = Ashu
Key = department, Value = Development
clear()函数用于从MutableMap中删除所有元素。例如:
fun main(args: Array) {
val mutableMap: MutableMap = mutableMapOf()
mutableMap.put("name", "Ashu")
mutableMap.put("city", "Delhi")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "Playing")
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = ${key}, Value = ${mutableMap[key]}")
}
println("......mutableMap.clear().......")
println(mutableMap.clear())
println(mutableMap)
}
输出:
......traverse mutableMap.......
Key = name, Value = Ashu
Key = city, Value = Delhi
Key = department, Value = Development
Key = hobby, Value = Playing
......mutableMap.clear().......
kotlin.Unit
{}