當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。