📜  Kotlin HashMap:hashMapOf()

📅  最后修改于: 2021-01-05 07:48:38             🧑  作者: Mango

Kotlin HashMap:hashMapOf()

hashMapOf()HashMap类的函数。它返回具有指定内容的新HashMap。它包含键和值形式的数据对。 HashMap是可变集合,可提供读写功能。

hashMapOf()函数的语法

inline fun  hashMapOf(): HashMap (source)
fun  hashMapOf(vararg pairs: Pair): HashMap (source)

Kotlin HashMap类的功能

Function Description
open fun put(key: K, value: V): V? It puts the specified key and value in the map
open operator fun get(key: K): V? It returns the value of specified key, or null if no such specified key is available in map.
open fun containsKey(key: K): Boolean It returns true if map contains specifies key.
open fun containsValue(value: V): Boolean It returns true if map maps one of more keys to specified value.
open fun clear() It removes all elements from map.
open fun remove(key: K): V? It removes the specified key and its corresponding value from map

Kotlin hashMapOf()示例1

HashMap的hashMapOf()函数可以声明为不同的通用类型,例如hashMapOf (),hashMapOf (),hashMapOf ()等。

fun main(args: Array){

    val intMap: HashMap = hashMapOf(1 to "Ashu",4 to "Rohan", 2 to "Ajeet", 3 to "Vijay")

    val stringMap: HashMap = hashMapOf("name" to "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")
    val anyMap: HashMap = hashMapOf(1 to "Ashu", "name" to "Rohsan", 2 to 200)
    println(".....traverse intMap........")
    for(key in intMap.keys){
        println(intMap[key])
    }
    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println(stringMap[key])
    }
    println("......traverse anyMap.......")
    for(key in anyMap.keys){
        println(anyMap[key])
    }
}

输出:

.....traverse intMap........
Ashu
Ajeet
Vijay
Rohan
......traverse stringMap.......
Ashu
Development
Delhi
Playing
......traverse anyMap.......
Rohsan
Ashu
200

Kotlin hashMapOf()示例2-containsKey()

如果containsKey()函数在HashMap中包含提及键,则返回true,否则返回false。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }

    println("......stringMap.containsKey(\"name\").......")
    println(stringMap.containsKey("name"))
}

输出:

......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.containsKey("name").......
true

Kotlin hashMapOf()示例3-containsValue()

如果containsValue()函数在HashMap中包含提及值,则返回true,否则返回false。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }

    println(".......stringMap.containsValue(\"Delhi\")......")
    println(stringMap.containsValue("Delhi"))
    println(stringMap.containsValue("Mumbai"))
}

输出:

......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
.......stringMap.containsValue("Delhi")......
true
false

Kotlin hashMapOf()示例4-contains()

如果contains()函数在HashMap中包含提及键,则返回true,否则返回false。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }

    println("......stringMap.contains(\"city\").......")
    println(stringMap.contains("city"))
}

输出:

......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.contains("city").......
true

Kotlin hashMapOf()示例5-replace(key,value)

replace(key,value)函数用于用新的指定值替换指定键处的现有值。 replace(key,value)函数返回替换后的值。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }

    println("......stringMap.replace(\"city\",\"Mumbai\").......")
    println(stringMap.replace("city","Mumbai"))
    println("......traverse stringMap after stringMap.replace(\"city\",\"Mumbai\").......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }
}

输出:

......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.replace("city","Mumbai").......
Delhi
......traverse stringMap after stringMap.replace("city","Mumbai").......
Key = city , value = Mumbai
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing

Kotlin hashMapOf()示例6-replace(key,oldValue,newValue)

replace(key,oldValue,newValue)函数用于用新的指定值替换指定键处的现有旧值。如果将旧值替换为新值,则replace(key,newValue,oldValue)函数返回true,否则返回false。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }

    println(".......stringMap.replace(\"department\", \"Development\",\"Management\")......")
    println(stringMap.replace("department", "Development","Management"))
    println("......traverse stringMap after stringMap.replace(\"department\", \"Development\",\"Management\").......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }
}

输出:

......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
.......stringMap.replace("department", "Development","Management")......
true
......traverse stringMap after stringMap.replace("department", "Development","Management").......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Management
Key = hobby , value = Playing

Kotlin hashMapOf()示例7-hashMapOf()。size,hashMapOf()。key

hashMapOf()函数的size属性返回HashMap的总大小,而key属性返回HashMap的所有键。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }

    println(".....stringMap.size........")
    println(stringMap.size)

    println(".......stringMap.keys......")
    println(stringMap.keys)
}

输出:

......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
.....stringMap.size........
4
.......stringMap.keys......
[city, name, department, hobby]

Kotlin hashMapOf()示例8-getValue(key),getOrDefault(key,defaultValue)

getValue()函数返回HashMap的指定键的值。而getOrDefault()函数返回指定键的对应值(如果该键存在于HashMap中),或者返回所提及的默认值(如果HashMap中不存在该键)。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }

    println(".......stringMap.getValue(\"department\")......")
    println(stringMap.getValue("department"))

    println(".......stringMap.getOrDefault(\"name\", \"Default Value\")......")
    println(stringMap.getOrDefault("name", "Default Value"))
}

输出:

......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
.......stringMap.getValue("department")......
Development
.......stringMap.getOrDefault("name", "Default Value")......
Ashu

Kotlin hashMapOf()示例9-remove(key)

remove(key)函数用于删除指定的键及其对应的值。 remove(key)函数返回删除的值。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }

        println("......stringMap.remove(\"city\").......")
        println(stringMap.remove("city"))
        println("......traverse stringMap after stringMap.remove(\"city\").......")
        for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }
}

输出:

......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.remove("city").......
Delhi
......traverse stringMap after stringMap.remove("city").......
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing

Kotlin hashMapOf()示例10-remove(key,value)

remove(key,value)函数用于删除指定的键及其对应的值。如果remove(key,value)函数将指定的键及其值一起移除,则返回true,否则返回false。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }
        println(".......stringMap.remove(\"hobby\",\"Playing\")......")
        println(stringMap.remove("hobby","Playing"))
    println("......traverse stringMap after stringMap.remove(\"hobby\",\"Playing\").......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }
}

输出:

......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
.......stringMap.remove("hobby","Playing")......
true
......traverse stringMap after stringMap.remove("hobby","Playing").......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development

Kotlin hashMapOf()示例11-set(key,value)

set(key,value)函数用于设置给定键处的给定值(如果存在)。如果该键在HashMap中不存在,它将添加新键并设置与之对应的给定值。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }

    stringMap.set("name","Ashutosh")
    stringMap.set("skill","Kotlin")
    println("......traverse stringMap after stringMap.set(\"name\",\"Ashutosh\") and   stringMap.set(\"skill\",\"Kotlin\").......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }
}

输出:

......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.set("name","Ashutosh").......
......traverse stringMap after stringMap.set("name","Ashutosh") and stringMap.set("skill","Kotlin").......
Key = city , value = Delhi
Key = skill , value = Kotlin
Key = name , value = Ashutosh
Key = department , value = Development
Key = hobby , value = Playing

Kotlin hashMapOf()示例12-clear()

clear()函数用于从HashMap中清除(或删除)所有键值对。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    println("......traverse stringMap.......")
    for(key in stringMap.keys){
        println("Key = ${key} , value = ${stringMap[key]}")
    }

        println("......stringMap.clear().......")
        println(stringMap.clear())
        println(stringMap)

}

输出:

......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.clear().......
kotlin.Unit
{}