📜  Kotlin mutableMapOf()

📅  最后修改于: 2022-05-13 01:55:00.920000             🧑  作者: Mango

Kotlin mutableMapOf()

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

mutableMapOf() or mutableMapOf ()

用于声明 MutableMap 接口

interface MutableMap : Map (source)

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

Java
fun main(args: Array) {
    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
}


Java
fun main(args : Array) {
 
    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())
}


Java
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)
}


Java
fun main(args: Array) {
    val mutableMap = mutableMapOf()
    mutableMap.put("Name", "Geek")
    mutableMap.put("Country", "India")
 
    val map = mapOf("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])
    }
}


Java
fun main(args: Array) {
 
    val mutableMap = mutableMapOf()
    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]}")
    }
}


Java
fun main(args: Array) {
 
    val mutableMap: MutableMap = mutableMapOf()
    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)
}


Java
fun main(args: Array) {     //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]}")     
    }
}


输出:

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

查找地图大小 -

我们可以使用两种方法来确定可变映射的大小。通过使用地图的 size 属性和使用 count() 方法。

Java

fun main(args : Array) {
 
    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 的值 –

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

Java

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 程序 –

Java

fun main(args: Array) {
    val mutableMap = mutableMapOf()
    mutableMap.put("Name", "Geek")
    mutableMap.put("Country", "India")
 
    val map = mapOf("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)函数–

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

Java

fun main(args: Array) {
 
    val mutableMap = mutableMapOf()
    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 程序 –

Java

fun main(args: Array) {
 
    val mutableMap: MutableMap = mutableMapOf()
    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 程序——

Java

fun main(args: Array) {     //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