Kotlin HashMap:hashMapOf()函数

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类的函数

函数 描述
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 <Int,String>()hashMapOf <String,String>()hashMapOf <Any,Any>()等。

fun main(args: Array<String>){

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

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

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

.....traverse intMap........
Java
Ajax
Vivi
Ruby
......traverse stringMap.......
Java
研发部
海口
专业撸码好手
......traverse anyMap.......
Ruby
Java
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", "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"))
}

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

......traverse stringMap.......
Key = city , Value = 海口
Key = name , Value = Java
Key = department , Value = 研发部
Key = hobby , Value = 专业撸码二级
......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", "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("上海"))
}

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

......traverse stringMap.......
Key = city , value = 海口
Key = name , value = Java
Key = department , value = 研发部
Key = hobby , value = 专业撸码
.......stringMap.containsValue("海口")......
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", "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"))
}

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

......traverse stringMap.......
Key = city , value = 海口
Key = name , value = Susen
Key = department , value = 研发
Key = hobby , value = 撸码
......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", "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]}")
    }
}

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

fun main(args: Array<String>){

    val stringMap: HashMap<String,String> = hashMapOf<String,String>()
    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 hashMapOf()示例6 - replace(key,oldValue,newValue)函数

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

fun main(args: Array<String>){

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

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

......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 = 撸码

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

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

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

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

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

fun main(args: Array<String>){

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

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

......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

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

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

......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 = 撸代码

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

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

......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 = 撸代码

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

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

......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 = 撸代码

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

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

fun main(args: Array<String>){

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

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

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

目录

数组

字符串

注解

反射