Kotlin教程:MutableMap接口

2023年 7月 12日 60.2k 0

Kotlin MutableMap是集合框架的接口,它以键和值对的形式保存对象。 通过使用相应的键来检索MutableMap接口的值。 键和值可以是不同类型的对,例如等等。MutableMap的每个键只保存一个值。

要使用MutableMap接口,我们需要使用它的函数:mutableMapOf()mutableMapOf ()

Kotlin MutableMap接口声明

interface MutableMap : Map (source)

Kotlin

MutableMap属性

属性 描述
MutableSet 这将返回映射中所有键和值对的MutableSet
MutableSet 这将返回此映射中MutableSet的所有键。
MutableCollection 这将返回当前映射中MutableCollection的所有值。 此集合可能包含重复值。

Kotlin MutableMap的函数

函数 描述
abstract fun put(key: K, value: V): V? 使用映射中的指定键添加给定值。
abstract fun putAll(from: Map) 使用指定映射中的键/值对更新当前映射。
abstract fun remove(key: K): V? 从映射中删除指定的键及对应的值。
open fun remove(key: K, value: V): Boolean 仅当映射中存在键和值实体时,它才会从映射中删除它们。
abstract fun clear() 此函数用于从映射中删除所有元素。
operator fun Map.contains(key: K): Boolean 它检查映射中是否包含给定键。
abstract fun containsKey(key: K): Boolean 如果map包含指定的键,则返回true
fun Map.containsKey(key: K): Boolean 如果map包含指定的键,则返回true
abstract fun containsValue(value: V): Boolean 如果映射包含给定值的一个或多个键,则返回true
fun Map.containsValue(value: V): Boolean 如果映射包含给定值的一个或多个键,则返回true
fun Map.count(): Int 它返回映射的项目总数。
operator fun Map.get(key: K): V? 它返回与键对应的值,如果在映射中找不到指定键,则返回null
fun Map.getOrDefault(key: K, defaultValue: V): V 它返回带有相应指定键的值,或者如果映射中没有键的映射,则返回默认值。
fun Map.getOrElse(key: K, defaultValue: () -> V ): V 它返回映射中提及键的值,或者如果找不到给定键的此类条目,则返回默认值函数。
fun Map.getValue(key: K): V 它返回与给定键对应的值,如果在映射中找不到键,则抛出异常。

Kotlin MutableMap示例 - 1遍历MutableMap

首先创建一个使用mutablemapOf()函数创建MutableMap并遍历它的示例。 在这个例子中,使用不同的方式(MutableMap ,MutableMap MutableMap )来创建MutableMap的三种不同类型。

fun main(args: Array) {

    val mutableMap1: MutableMap = mutableMapOf(1 to "Java", 4 to "Ruby", 2 to "Ajax", 3 to "Vivi")
    val mutableMap2: MutableMap = mutableMapOf()
    mutableMap2.put("name", "Susen")
    mutableMap2.put("city", "海口")
    mutableMap2.put("department", "研发部")
    mutableMap2.put("hobby", "撸代码")
    val mutableMap3: MutableMap = mutableMapOf(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]}")
    }
}

Kotlin

执行上面示例代码,得到以下结果 -

.....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

Shell

Kotlin MutableMap示例2 - put()和putAll()函数

put()putAll()函数用于添加MutableMap中的元素。put()函数向MutableMap中添加单个元素,putAll()函数向MutableMap添加集合类型元素。例如:

fun main(args: Array) {

    val mutableMap: MutableMap = mutableMapOf()
    mutableMap.put("name", "Susen")
    mutableMap.put("city", "海口")

    val hashMap: HashMap = hashMapOf()
    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])
    }
}

Kotlin

执行上面示例代码,得到以下结果 -

......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 = 撸代码

Shell

Kotlin MutableMap示例3 - containsKey()函数

containsKey()函数用于检查MutableMap中是否存在指定的键。 如果它包含指定的键,则返回true,否则返回false。 例如:

fun main(args: Array) {

    val mutableMap: MutableMap = mutableMapOf()
    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"))
}

Kotlin

执行上面示例代码,得到以下结果 -

......traverse mutableMap.......
Key = name, Value = Maxsu
Key = city, Value = 海口
Key = department, Value = 研发部
Key = hobby, Value = 女
......mutableMap.containsKey("city").......
true

Shell

Kotlin MutableMap示例4 - containsValue()函数

containsValue()函数用于检查MutableMap中是否存在指定的值。 如果映射包含给定的一个或多个值,则此函数返回true,否则返回false。 例如:

fun main(args: Array) {

    val mutableMap: MutableMap = mutableMapOf()
    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"))
}

Kotlin

执行上面示例代码,得到以下结果 -

......traverse mutableMap.......
Key = name, Value = Maxsu
Key = city, Value = 海口
Key = department, Value = 研发部
Key = hobby, Value = 撸代码
.......mutableMap.containsValue("上海")......
false
.......mutableMap.containsValue("Maxsu")......
true

Shell

Kotlin MutableMap示例5 - contains()函数

contains()函数用于检查MutableMap中是否存在指定的值键。如果指定的键或值存在于MutableMap中,则它将返回true,否则返回false。 例如:

fun main(args: Array) {

    val mutableMap: MutableMap = mutableMapOf()
    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"))

}

Kotlin

执行上面示例代码,得到以下结果 -

......traverse mutableMap.......
Key = name, Value = Susen
Key = city, Value = Haikou
Key = department, Value = Development
Key = hobby, Value = Coding
......mutableMap.contains("city").......
true

Shell

Kotlin MutableMap示例6 - get(key)函数

get(key)函数用于检索MutableMap中指定键的对应值。如果MutableMap中没有包含指定键,则返回null。 例如:

fun main(args: Array) {

    val mutableMap: MutableMap = mutableMapOf()
    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"))

}

Kotlin

执行上面示例代码,得到以下结果 -

......traverse mutableMap.......
Key = name, Value = Susen
Key = city, Value = Haikou
Key = department, Value = Development
Key = hobby, Value = Coding
.......mutableMap.get("department")......
Development

Shell

Kotlin MutableMap示例7 - getValue(key)函数

getValue()函数用于返回MutableMap的指定键的相应值,如果在map中找不到键,则抛出异常。 例如:

fun main(args: Array) {

    val mutableMap: MutableMap = mutableMapOf()
    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"))
}

Kotlin

执行上面示例代码,得到以下结果 -

......traverse mutableMap.......
Key = name, Value = Maxsu
Key = city, Value = 海口
Key = department, Value = Development
Key = hobby, Value = 撸代码
.......mutableMap.getValue("department")......
Development

Shell

Kotlin MutableMap示例8 - getOrDefault()函数

getOrDefault()函数返回MutableMap指定键的对应值。 如果MutableMap中不存在指定的键,则返回默认对应值。 例如:

fun main(args: Array) {

    val mutableMap: MutableMap = mutableMapOf()
    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"))

}

Kotlin

执行上面示例代码,得到以下结果 -

......traverse mutableMap.......
Key = name, Value = Maxsu
Key = city, Value = 海口
Key = department, Value = Development
Key = hobby, Value = 撸代码
.......mutableMap.getOrDefault("name", "Default Value")......
Maxsu

Shell

Kotlin MutableMap示例9 - count()函数

count()函数用于返回MutableMap中存在的元素总数。 例如:

fun main(args: Array) {

    val mutableMap: MutableMap = mutableMapOf()
    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())
}

Kotlin

执行上面示例代码,得到以下结果 -

......traverse mutableMap.......
Key = name, Value = Maxsu
Key = city, Value = 海口
Key = department, Value = Development
Key = hobby, Value = 撸代码
.....mutableMap.count()........
4

Shell

Kotlin MutableMap示例10 - remove(key) 和 remove(key, value)函数

remove(key)函数用于删除与指定键对应的值。 而remove(key,value)函数删除包含键和值的元素。 如果成功删除指定的键及其值,则remove(key,value)函数返回true,否则返回false。 例如:

fun main(args: Array) {

    val mutableMap: MutableMap = mutableMapOf()
    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]}")
    }
}

Kotlin

执行上面示例代码,得到以下结果 -

......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

Shell

Kotlin MutableMap示例11 - clear()函数

clear()函数用于从MutableMap中删除所有元素。 例如:

fun main(args: Array) {

    val mutableMap: MutableMap = mutableMapOf()
    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)
}

Kotlin

执行上面示例代码,得到以下结果 -

......traverse mutableMap.......
Key = name, Value = Maxsu
Key = city, Value = 海口
Key = department, Value = 技术部
Key = hobby, Value = 撸代码
......mutableMap.clear().......
kotlin.Unit
{}

//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/kotlin/kotlin-mutablemap.html#article-start

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论