Kotlin教程:HashMap:hashMapOf()函数

2023年 7月 12日 58.5k 0

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

hashMapOf()函数的语法

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

Kotlin

Kotlin HashMap类的函数

函数 描述
open fun put(key: K, value: V): V? 它将指定的键和值放在Map中
open operator fun get(key: K): V? 它返回指定键的值,如果map中没有这样的指定键,则返回null
open fun containsKey(key: K): Boolean 如果map包函指定键,则返回true
open fun containsValue(value: V): Boolean 如果map将一个或多个键映射到指定值,则返回true
open fun clear() 它从Map中删除所有元素。
open fun remove(key: K): V? 它从map中删除指定的键及其对应的值

Kotlin hashMapOf()示例1

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

fun main(args: Array){

    val intMap: HashMap = hashMapOf(1 to "Java",4 to "Ruby", 2 to "Ajax", 3 to "Vivi")

    val stringMap: HashMap = hashMapOf("name" to "Java")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发部")
    stringMap.put("hobby", "专业撸码好手")
    val anyMap: HashMap = hashMapOf(1 to "Java", "name" to "Ruby", 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])
    }
}

Kotlin

执行上面示例代码,得到以下结果 -

.....traverse intMap........
Java
Ajax
Vivi
Ruby
......traverse stringMap.......
Java
研发部
海口
专业撸码好手
......traverse anyMap.......
Ruby
Java
200

Shell

Kotlin hashMapOf()示例2 - containsKey()函数

containsKey()函数判断在HashMap中是否包含指定的键,有包函则返回true,否则返回false

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Java")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发部")
    stringMap.put("hobby", "专业撸码二级")

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

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

Kotlin

执行上面示例代码,得到以下结果 -

......traverse stringMap.......
Key = city , Value = 海口
Key = name , Value = Java
Key = department , Value = 研发部
Key = hobby , Value = 专业撸码二级
......stringMap.containsKey("name").......
true

Shell

Kotlin hashMapOf()示例3 - containsValue()函数

containsValue()函数检查是否在HashMap中包含指定值,包含则返回true,否则返回false

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Java")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发部")
    stringMap.put("hobby", "专业撸码")

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

    println(".......stringMap.containsValue(\"海口\")......")
    println(stringMap.containsValue("海口"))
    println(stringMap.containsValue("上海"))
}

Kotlin

执行上面示例代码,得到以下结果 -

......traverse stringMap.......
Key = city , value = 海口
Key = name , value = Java
Key = department , value = 研发部
Key = hobby , value = 专业撸码
.......stringMap.containsValue("海口")......
true
false

Shell

Kotlin hashMapOf()示例4 - contains()函数

contains()函数检查是否在HashMap中包含指定键,包含则返回true,否则返回false

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Susen")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发")
    stringMap.put("hobby", "撸码")

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

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

Kotlin

执行上面示例代码,得到以下结果 -

......traverse stringMap.......
Key = city , value = 海口
Key = name , value = Susen
Key = department , value = 研发
Key = hobby , value = 撸码
......stringMap.contains("city").......
true

Shell

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

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

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Susan")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发")
    stringMap.put("hobby", "撸码")

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

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

Kotlin

执行上面示例代码,得到以下结果 -

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Susan")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发")
    stringMap.put("hobby", "撸码")

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

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

Shell

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

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

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Susan")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发")
    stringMap.put("hobby", "撸码")

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

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

Kotlin

执行上面示例代码,得到以下结果 -

......traverse stringMap.......
Key = city , value = 海口
Key = name , value = Susan
Key = department , value = 研发
Key = hobby , value = 撸码
......stringMap.replace("city","北京").......
海口
......traverse stringMap after stringMap.replace("city","北京").......
Key = city , value = 北京
Key = name , value = Susan
Key = department , value = 研发
Key = hobby , value = 撸码

Shell

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

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

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Susen")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发")
    stringMap.put("hobby", "撸代码")

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

Kotlin

执行上面示例代码,得到以下结果 -

......traverse stringMap.......
Key = city , value = 海口
Key = name , value = Susen
Key = department , value = 研发
Key = hobby , value = 撸代码
.....stringMap.size........
4
.......stringMap.keys......
[city, name, department, hobby]

Shell

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

getValue()函数返回HashMap的指定键的值。 getOrDefault()函数返回指定键的相应值(如果它存在于HashMap中),或者如果HashMap中不存在指定键则返回给定的默认值(defaultValue)。

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "苏生")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发部")
    stringMap.put("hobby", "撸代码")

    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"))

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

Kotlin

执行上面示例代码,得到以下结果 -

......traverse stringMap.......
Key = city , value = 海口
Key = name , value = 苏生
Key = department , value = 研发部
Key = hobby , value = 撸代码
.......stringMap.getValue("department")......
研发部
.......stringMap.getOrDefault("name", "Default Value")......
苏生
.......stringMap.getOrDefault("name", "Default Value")......
Default-Value

Shell

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

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

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Susen")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发部")
    stringMap.put("hobby", "撸代码")

    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]}")
    }
}

Kotlin

执行上面示例代码,得到以下结果 -

......traverse stringMap.......
Key = city , value = 海口
Key = name , value = Susen
Key = department , value = 研发部
Key = hobby , value = 撸代码
......stringMap.remove("city").......
海口
......traverse stringMap after stringMap.remove("city").......
Key = name , value = Susen
Key = department , value = 研发部
Key = hobby , value = 撸代码

Shell

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", "Susan")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发部")
    stringMap.put("hobby", "撸代码")

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

Kotlin

执行上面示例代码,得到以下结果 -

......traverse stringMap.......
Key = city , value = 海口
Key = name , value = Susan
Key = department , value = 研发部
Key = hobby , value = 撸代码
.......stringMap.remove("hobby","撸代码")......
false
......traverse stringMap after stringMap.remove("hobby","撸代码").......
Key = city , value = 海口
Key = name , value = Susan
Key = department , value = 研发部
Key = hobby , value = 撸代码

Shell

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

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

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Susan")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发部")
    stringMap.put("hobby", "撸代码")

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

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

Kotlin

执行上面示例代码,得到以下结果 -

......traverse stringMap.......
Key = city , value = 海口
Key = name , value = Susan
Key = department , value = 研发部
Key = hobby , value = 撸代码
......traverse stringMap after stringMap.set("name","Maxsu") and   stringMap.set("skill","Kotlin").......
Key = city , value = 海口
Key = skill , value = Kotlin
Key = name , value = Maxsu
Key = department , value = 研发部
Key = hobby , value = 撸代码

Shell

Kotlin hashMapOf()示例12 - clear()函数

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

fun main(args: Array){

    val stringMap: HashMap = hashMapOf()
    stringMap.put("name", "Susan")
    stringMap.put("city", "海口")
    stringMap.put("department", "研发部")
    stringMap.put("hobby", "撸代码")

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

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

Kotlin

执行上面示例代码,得到以下结果 -

......traverse stringMap.......
Key = city , value = 海口
Key = name , value = Susan
Key = department , value = 研发部
Key = hobby , value = 撸代码
......stringMap.clear().......
kotlin.Unit
{}

//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/kotlin/kotlin-hashmap-hashmapof.html#article-start

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论