Kotlin MutableMap接口
Kotlin MutableMap
是集合框架的接口,它以鍵和值對的形式保存對象。 通過使用相應的鍵來檢索MutableMap
接口的值。 鍵和值可以是不同類型的對,例如<Int,Int>
,<Int,String>
,<Char,String>
等等。MutableMap
的每個鍵只保存一個值。
要使用MutableMap
接口,我們需要使用它的函數:mutableMapOf()
或mutableMapOf <k,v>()
。
Kotlin MutableMap接口聲明
interface MutableMap<K, V> : Map<K, V> (source)
MutableMap屬性
屬性
描述
MutableSet<MutableEntry<K, V>>
這將返回映射中所有鍵和值對的MutableSet
。
MutableSet<K>
這將返回此映射中MutableSet
的所有鍵。
MutableCollection<V>
這將返回當前映射中MutableCollection
的所有值。 此集合可能包含重複值。
Kotlin MutableMap的函數
函數
描述
abstract fun put(key: K, value: V): V?
使用映射中的指定鍵添加給定值。
abstract fun putAll(from: Map<out K, V>)
使用指定映射中的鍵/值對更新當前映射。
abstract fun remove(key: K): V?
從映射中刪除指定的鍵及對應的值。
open fun remove(key: K, value: V): Boolean
僅當映射中存在鍵和值實體時,它纔會從映射中刪除它們。
abstract fun clear()
此函數用於從映射中刪除所有元素。
operator fun <K, V> Map<out K, V>.contains(key: K): Boolean
它檢查映射中是否包含給定鍵。
abstract fun containsKey(key: K): Boolean
如果map
包含指定的鍵,則返回true
。
fun <K> Map<out K, *>.containsKey(key: K): Boolean
如果map
包含指定的鍵,則返回true
。
abstract fun containsValue(value: V): Boolean
如果映射包含給定值的一個或多個鍵,則返回true
。
fun <K, V> Map<K, V>.containsValue(value: V): Boolean
如果映射包含給定值的一個或多個鍵,則返回true
。
fun <K, V> Map<out K, V>.count(): Int
它返回映射的項目總數。
operator fun <K, V> Map<out K, V>.get(key: K): V?
它返回與鍵對應的值,如果在映射中找不到指定鍵,則返回null
。
fun <K, V> Map<out K, V>.getOrDefault(key: K, defaultValue: V): V
它返回帶有相應指定鍵的值,或者如果映射中沒有鍵的映射,則返回默認值。
fun <K, V> Map<K, V>.getOrElse(key: K, defaultValue: () -> V ): V
它返回映射中提及鍵的值,或者如果找不到給定鍵的此類條目,則返回默認值函數。
fun <K, V> Map<K, V>.getValue(key: K): V
它返回與給定鍵對應的值,如果在映射中找不到鍵,則拋出異常。
Kotlin MutableMap示例 - 1遍歷MutableMap
首先創建一個使用mutablemapOf()
函數創建MutableMap
並遍歷它的示例。 在這個例子中,使用不同的方式(MutableMap <Int,String>,MutableMap <String,String>
和MutableMap <Any,Any>)
來創建MutableMap
的三種不同類型。
fun main(args: Array<String>) {
val mutableMap1: MutableMap<Int, String> = mutableMapOf<Int, String>(1 to "Java", 4 to "Ruby", 2 to "Ajax", 3 to "Vivi")
val mutableMap2: MutableMap<String, String> = mutableMapOf<String, String>()
mutableMap2.put("name", "Susen")
mutableMap2.put("city", "海口")
mutableMap2.put("department", "研發部")
mutableMap2.put("hobby", "擼代碼")
val mutableMap3: MutableMap<Any, Any> = mutableMapOf<Any, Any>(1 to "Maxsu", "name" to "Ruby", 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 = Java
Key = 4, Value = Ruby
Key = 2, Value = Ajax
Key = 3, Value = Vivi
......traverse mutableMap2.......
Key = name, Value = Susen
Key = city, Value = 海口
Key = department, Value = 研發部
Key = hobby, Value = 擼代碼
......traverse mutableMap3......
Key = 1, Value = Maxsu
Key = name, Value = Ruby
Key = 2, Value = 200
Kotlin MutableMap示例2 - put()和putAll()函數
put()
和putAll()
函數用於添加MutableMap
中的元素。put()
函數向MutableMap中添加單個元素,putAll()
函數向MutableMap
添加集合類型元素。例如:
fun main(args: Array<String>) {
val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
mutableMap.put("name", "Susen")
mutableMap.put("city", "海口")
val hashMap: HashMap<String,String> = hashMapOf<String,String>()
hashMap.put("department", "研發部")
hashMap.put("hobby", "擼代碼")
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 = Susen
Key = city, Value = 海口
......traverse mutableMap after mutableMap.putAll(hashMap).......
Key = name, Value = Susen
Key = city, Value = 海口
Key = department, Value = 研發部
Key = hobby, Value = 擼代碼
Kotlin MutableMap示例3 - containsKey()函數
containsKey()
函數用於檢查MutableMap中是否存在指定的鍵。 如果它包含指定的鍵,則返回true
,否則返回false
。 例如:
fun main(args: Array<String>) {
val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
mutableMap.put("name", "Maxsu")
mutableMap.put("city", "海口")
mutableMap.put("department", "研發部")
mutableMap.put("hobby", "女")
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 = Maxsu
Key = city, Value = 海口
Key = department, Value = 研發部
Key = hobby, Value = 女
......mutableMap.containsKey("city").......
true
Kotlin MutableMap示例4 - containsValue()函數
containsValue()
函數用於檢查MutableMap中是否存在指定的值。 如果映射包含給定的一個或多個值,則此函數返回true
,否則返回false
。 例如:
fun main(args: Array<String>) {
val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
mutableMap.put("name", "Maxsu")
mutableMap.put("city", "海口")
mutableMap.put("department", "研發部")
mutableMap.put("hobby", "擼代碼")
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = "+key +", "+"Value = "+mutableMap[key])
}
println(".......mutableMap.containsValue(\"上海\")......")
println(mutableMap.containsValue("上海"))
println(".......mutableMap.containsValue(\"Maxsu\")......")
println(mutableMap.containsValue("Maxsu"))
}
執行上面示例代碼,得到以下結果 -
......traverse mutableMap.......
Key = name, Value = Maxsu
Key = city, Value = 海口
Key = department, Value = 研發部
Key = hobby, Value = 擼代碼
.......mutableMap.containsValue("上海")......
false
.......mutableMap.containsValue("Maxsu")......
true
Kotlin MutableMap示例5 - contains()函數
contains()
函數用於檢查MutableMap中是否存在指定的值鍵。如果指定的鍵或值存在於MutableMap中,則它將返回true
,否則返回false
。 例如:
fun main(args: Array<String>) {
val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
mutableMap.put("name", "Susen")
mutableMap.put("city", "Haikou")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "Coding")
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 = Susen
Key = city, Value = Haikou
Key = department, Value = Development
Key = hobby, Value = Coding
......mutableMap.contains("city").......
true
Kotlin MutableMap示例6 - get(key)函數
get(key)
函數用於檢索MutableMap
中指定鍵的對應值。如果MutableMap
中沒有包含指定鍵,則返回null
。 例如:
fun main(args: Array<String>) {
val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
mutableMap.put("name", "Susen")
mutableMap.put("city", "Haikou")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "Coding")
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 = Susen
Key = city, Value = Haikou
Key = department, Value = Development
Key = hobby, Value = Coding
.......mutableMap.get("department")......
Development
Kotlin MutableMap示例7 - getValue(key)函數
getValue()
函數用於返回MutableMap
的指定鍵的相應值,如果在map
中找不到鍵,則拋出異常。 例如:
fun main(args: Array<String>) {
val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
mutableMap.put("name", "Maxsu")
mutableMap.put("city", "海口")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "擼代碼")
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 = Maxsu
Key = city, Value = 海口
Key = department, Value = Development
Key = hobby, Value = 擼代碼
.......mutableMap.getValue("department")......
Development
Kotlin MutableMap示例8 - getOrDefault()函數
getOrDefault()
函數返回MutableMap
指定鍵的對應值。 如果MutableMap
中不存在指定的鍵,則返回默認對應值。 例如:
fun main(args: Array<String>) {
val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
mutableMap.put("name", "Maxsu")
mutableMap.put("city", "海口")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "擼代碼")
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 = Maxsu
Key = city, Value = 海口
Key = department, Value = Development
Key = hobby, Value = 擼代碼
.......mutableMap.getOrDefault("name", "Default Value")......
Maxsu
Kotlin MutableMap示例9 - count()函數
count()
函數用於返回MutableMap
中存在的元素總數。 例如:
fun main(args: Array<String>) {
val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
mutableMap.put("name", "Maxsu")
mutableMap.put("city", "海口")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "擼代碼")
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 = Maxsu
Key = city, Value = 海口
Key = department, Value = Development
Key = hobby, Value = 擼代碼
.....mutableMap.count()........
4
Kotlin MutableMap示例10 - remove(key) 和 remove(key, value)函數
remove(key)
函數用於刪除與指定鍵對應的值。 而remove(key,value)
函數刪除包含鍵和值的元素。 如果成功刪除指定的鍵及其值,則remove(key,value)
函數返回true
,否則返回false
。 例如:
fun main(args: Array<String>) {
val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
mutableMap.put("name", "Maxsu")
mutableMap.put("city", "海口")
mutableMap.put("department", "Development")
mutableMap.put("hobby", "擼代碼")
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\",\"擼代碼\")......")
println(mutableMap.remove("hobby","擼代碼"))
println("......traverse mutableMap.......")
for (key in mutableMap.keys) {
println("Key = ${key}, Value = ${mutableMap[key]}")
}
}
執行上面示例代碼,得到以下結果 -
......traverse mutableMap.......
Key = name, Value = Maxsu
Key = city, Value = 海口
Key = department, Value = Development
Key = hobby, Value = 擼代碼
......mutableMap.remove("city").......
海口
.......mutableMap.remove("hobby","擼代碼")......
true
......traverse mutableMap.......
Key = name, Value = Maxsu
Key = department, Value = Development
Kotlin MutableMap示例11 - clear()函數
clear()
函數用於從MutableMap
中刪除所有元素。 例如:
fun main(args: Array<String>) {
val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
mutableMap.put("name", "Maxsu")
mutableMap.put("city", "海口")
mutableMap.put("department", "技術部")
mutableMap.put("hobby", "擼代碼")
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 = Maxsu
Key = city, Value = 海口
Key = department, Value = 技術部
Key = hobby, Value = 擼代碼
......mutableMap.clear().......
kotlin.Unit
{}