Kotlin MutableMap
是集合框架的介面,它以鍵和值對的形式儲存物件。 通過使用相應的鍵來檢索MutableMap
介面的值。 鍵和值可以是不同型別的對,例如<Int,Int>
,<Int,String>
,<Char,String>
等等。MutableMap
的每個鍵只儲存一個值。
要使用MutableMap
介面,我們需要使用它的函式:mutableMapOf()
或mutableMapOf <k,v>()
。
interface MutableMap<K, V> : Map<K, V> (source)
屬性 | 描述 |
---|---|
MutableSet<MutableEntry<K, V>> |
這將返回對映中所有鍵和值對的MutableSet 。 |
MutableSet<K> |
這將返回此對映中MutableSet 的所有鍵。 |
MutableCollection<V> |
這將返回當前對映中MutableCollection 的所有值。 此集合可能包含重複值。 |
函式 | 描述 |
---|---|
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 |
它返回與給定鍵對應的值,如果在對映中找不到鍵,則丟擲異常。 |
首先建立一個使用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
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 = 擼程式碼
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
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
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
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
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
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
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
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
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
{}