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
{}

上一篇:Kotlin HashMap:hashMapOf()函数

下一篇:Kotlin Set接口

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程