个性化阅读
专注于IT技术分析

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的属性

物产 描述
抽象val条目:MutableSet <MutableEntry <K, V >> 这将返回映射中所有键和值对的MutableSet。
抽象val键:MutableSet <K> 这将返回此映射中MutableSet的所有键。
抽象val值:MutableCollection <V> 这将返回当前映射中MutableCollection的所有值。此集合可能包含重复的值。

Kotlin MutableMap的功能

功能 描述
抽象有趣的put(key:K, value:V):V? 它将给定值与映射中的指定键相加。
抽象乐趣putAll(来自:Map <out K, V>) 这将使用上述映射中的键/值对更新当前映射。
抽象乐趣remove(key:K):V? 它将指定的键及其对应的值从映射中删除。
open fun remove(key:K, value:V):布尔值 仅当键和值实体存在于映射中时, 它才会从映射中删除。
abstract fun clear() 此功能用于从地图上删除所有元素。
运算符fun <K, V> Map <out K, V> .contains(key:K):布尔值 它检查地图中的给定键。
抽象乐趣containsKey(key:K):布尔 如果map包含指定的键, 则返回true。
fun <K> Map <out K, *>。containsKey(key:K):布尔值 如果map包含指定的键, 则返回true。
抽象乐趣containsValue(value:V):布尔 如果地图为给定值映射一个或多个键, 则返回true。
fun <K, V>映射<K, V> .containsValue(value:V):布尔值 如果地图为给定值映射一个或多个键, 则返回true。
fun <K, V>映射<out K, V> .count():Int 它返回地图的实体总数
运算符fun <K, V>映射<out K, V> .get(key:K):V? 它返回对应于提及键的值;如果在地图中找不到这样的键, 则返回null。
fun <K, V>映射<out K, V> .getOrDefault(key:K, defaultValue:V):V 它返回带有相应提及键的值, 或者如果映射中没有针对键的此类映射, 则返回默认值。
fun <K, V>映射<K, V> .getOrElse(键:K, defaultValue:()-> V):V 它返回映射中提及键的值, 或者如果找不到给定键的此类条目, 则返回默认值函数。
有趣的<K, V>映射<K, V> .getValue(key:K):V 它返回与给定键对应的值, 如果在映射中找不到键, 则抛出异常。

Kotlin MutableMap示例-遍历1个MutableMap

让我们创建一个示例, 使用mutablemapOf()函数创建一个MutableMap并遍历它。在此示例中, 我们以不同的方式创建了MutableMap的三种不同类型(MutableMap <Int, String>, MutableMap <String, String>和MutableMap <Any, Any>)。

fun main(args: Array<String>) {

    val mutableMap1: MutableMap<Int, String> = mutableMapOf<Int, String>(1 to "Ashu", 4 to "Rohan", 2 to "Ajeet", 3 to "Vijay")

    val mutableMap2: MutableMap<String, String> = mutableMapOf<String, String>()
    mutableMap2.put("name", "Ashu")
    mutableMap2.put("city", "Delhi")
    mutableMap2.put("department", "Development")
    mutableMap2.put("hobby", "Playing")
    val mutableMap3: MutableMap<Any, Any> = mutableMapOf<Any, Any>(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

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

函数put()和putAll()用于在MutableMap中添加元素。 put()函数一次添加单个元素, 而putAll()函数在MutableMap中添加集合类型元素。例如:

fun main(args: Array<String>) {

    val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
    mutableMap.put("name", "Ashu")
    mutableMap.put("city", "Delhi")


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

Kotlin MutableMap示例-3 containsKey()

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

fun main(args: Array<String>) {

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

Kotlin MutableMap示例-4 containsValue()

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

fun main(args: Array<String>) {

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

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", "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

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", "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

Kotlin MutableMap示例-7 getValue(key)

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

fun main(args: Array<String>) {

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

Kotlin MutableMap示例-8 getOrDefault()

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

fun main(args: Array<String>) {

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

Kotlin MutableMap示例-9 count()

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

fun main(args: Array<String>) {

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

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", "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

Kotlin MutableMap示例-11 clear()

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

fun main(args: Array<String>) {

    val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
    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
{}
赞(0)
未经允许不得转载:srcmini » Kotlin MutableMap接口

评论 抢沙发

评论前必须登录!