当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Kotlin mutableMapOf()用法及代码示例


科特林MutableMap是集合框架的接口,其中包含键和值形式的对象。它允许用户有效地检索每个键对应的值。键和值可以是不同的对,例如 <Int, String>、< Char, String> 等。
为了使用MutableMap接口,我们需要使用如下所示的函数

mutableMapOf() or mutableMapOf <K, V>()

用于声明MutableMap接口

interface MutableMap<K, V> : Map<K, V> (source)

包含条目、键、值的可变函数示例。

科特林


fun main(args: Array<String>) {
    val items = mutableMapOf("Box" to 12, "Books" to 18, "Table" to 13)
    println("Entries: " + items.entries)       //Printing Entries
    println("Keys:" + items.keys)              //Printing Keys
    println("Values:" + items.values)          //Printing Values
}

输出:

Entries: [Box=12, Books=18, Table=13]
Keys:[Box, Books, Table]
Values:[12, 18, 13]

查找Map尺寸 -

我们可以使用两种方法确定可变映射的大小。通过使用Map的大小属性并使用count()方法。

科特林


fun main(args : Array<String>) {
    val ranks = mutableMapOf(1 to "India",2 to "Australia",
        3 to "England",4 to "Africa")
    //method 1
    println("The size of the mutablemap is: "+ranks.size)
    //method 2
    println("The size of the mutablemap is: "+ranks.count())
}

输出:

The size of the mutablemap is: 4
The size of the mutablemap is: 4

获取 Map 的值 -

我们可以使用下面程序中讨论的不同方法从可变映射中检索值。

科特林


fun main() {
    val ranks = mutableMapOf(1 to "India",2 to "Australia",
        3 to "England",4 to "Africa")
    //method 1
    println("Team having rank #1 is: "+ranks[1])
    //method 2
    println("Team having rank #3 is: "+ranks.getValue(3))
    //method 3
    println("Team having rank #4 is: "+ranks.getOrDefault(4, 0))
    // method  4
    val team = ranks.getOrElse(2 ,{ 0 })
    println(team)
}

输出:

Team having rank #1 is: India
Team having rank #3 is: England
Team having rank #4 is: Africa
Australia

put()和putAll()函数

put() 和 putAll() 函数用于在 MutableMap 中添加元素。put() 函数一次添加单个元素,而 putAll() 函数可用于在 MutableMap 中一次添加多个元素。
使用put()和putAll()函数的Kotlin程序 -

科特林


fun main(args: Array<String>) {
    val mutableMap = mutableMapOf<String, String>()
    mutableMap.put("Name", "Geek")
    mutableMap.put("Country", "India")
    val map = mapOf<String,String>("Department" to "Computer Science",
        "Hobby" to "Coding")
    println("<----Traverse mutableMap---->")
    for (key in mutableMap.keys) {
        println("Key = "+key +", "+"Value = "+mutableMap[key])
    }
    mutableMap.putAll(map)
    println("<----Traversal after putting hashmap---->")
    for (key in mutableMap.keys) {
        println("Key = "+key +", "+"Value = "+mutableMap[key])
    }
}

输出:

<----Traverse mutableMap---->
Key = Name, Value = Geek
Key = Country, Value = India
<----Traversal after putting hashmap---->
Key = Name, Value = Geek
Key = Country, Value = India
Key = Department, Value = Computer Science
Key = Hobby, Value = Coding

删除(键)和删除(键,值)函数 -

函数remove(key)用于删除与其提到的键对应的值。
函数remove(key, value)用于删除包含键和值的元素
Kotlin 程序演示remove() 函数 -

科特林


fun main(args: Array<String>) {
    val mutableMap = mutableMapOf<String, String>()
    mutableMap.put("Name", "Geek")
    mutableMap.put("Company", "GeeksforGeeks")
    mutableMap.put("Country", "India")
    for (key in mutableMap.keys) {
        println("Key = ${key}, Value = ${mutableMap[key]}")
    }
    println()
    println("Remove the Key: "+mutableMap.remove("Country"))
    println()
    println("Is pair removes from the map: "
            +mutableMap.remove("Company","GeeksforGeeks"))
    println()
    println("<---Traverse Again--->")
    for (key in mutableMap.keys) {
        println("Key = ${key}, Value = ${mutableMap[key]}")
    }
}

输出:

Key = Name, Value = Geek
Key = Company, Value = GeeksforGeeks
Key = Country, Value = India

Remove the Key: India

Is pair removes from the map: true

<---Traverse Again--->
Key = Name, Value = Geek

clear()函数-

用于从 mutableMap 中删除所有元素。
使用clear()函数的Kotlin程序 -

科特林


fun main(args: Array<String>) {
    val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>()
    mutableMap.put("Name", "Geek")
    mutableMap.put("Company", "GeeksforGeeks")
    for (key in mutableMap.keys) {
        println("Key = ${key}, Value = ${mutableMap[key]}")
    }
    println("mutableMap.clear()")
    println("Method called to clear the map: "+mutableMap.clear())
    println("Map Empty: "+mutableMap)
}

输出:

Key = Name, Value = Geek
Key = Company, Value = GeeksforGeeks
mutableMap.clear()
Method called to clear the map: kotlin.Unit
Map Empty: {}

在可变映射中遍历 -

遍历意味着遍历链表、数组、树等数据结构中的每个节点。
它只是意味着至少访问每个节点一次以将其显示给用户或对其执行操作。
为了理解这一点,我们举一个例子
Kotlin 程序演示遍历 -

科特林


fun main(args: Array<String>) {     //Declaring function
    //Creating MutableMap of different types
    val mutableMap = mutableMapOf(1 to "Aditya",
        4 to "Vilas", 2 to "Manish", 3 to "Manjot")
    for (key in mutableMap.keys) {
        println("Key = ${key}, Value = ${mutableMap[key]}")      
    }
}

输出:

Key = 1, Value = Aditya
Key = 4, Value = Vilas
Key = 2, Value = Manish
Key = 3, Value = Manjot


相关用法


注:本文由纯净天空筛选整理自Lakhiwal大神的英文原创作品 Kotlin mutableMapOf()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。