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

Kotlin HashMap:hashMapOf()

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

hashMapOf()函数的语法

inline fun <K, V> hashMapOf(): HashMap<K, V> (source)
fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V> (source)

Kotlin HashMap类的功能

功能 描述
打开趣味看跌期权(键:K, 值:V):V? 它将指定的键和值放入地图中
打开运算符fun get(key:K):V? 它返回指定键的值;如果map中没有这样的指定键, 则返回null。
打开乐趣containsKey(key:K):布尔值 如果map包含指定键, 则返回true。
打开乐趣containsValue(value:V):布尔值 如果map将多个键之一映射到指定值, 则返回true。
open fun clear() 它从地图中删除所有元素。
打开乐趣删除(键:K):V? 它从映射中删除指定的键及其对应的值

Kotlin hashMapOf()示例1

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

fun main(args: Array<String>){

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

    val stringMap: HashMap<String, String> = hashMapOf<String, String>("name" to "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")
    val anyMap: HashMap<Any, Any> = hashMapOf<Any, Any>(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<String>){

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

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

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

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

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

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

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

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

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

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

    val stringMap: HashMap<String, String> = hashMapOf<String, String>()
    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
{}
赞(0)
未经允许不得转载:srcmini » Kotlin HashMap:hashMapOf()

评论 抢沙发

评论前必须登录!