Kotlin HashMap: hashMapOf()

2024年8月29日 | 阅读 7 分钟

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

hashMapOf() 函数的语法

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>() 等。

输出

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

输出

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

输出

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

输出

......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) 函数返回替换后的值。

输出

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

输出

......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 的所有键。

输出

......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 中不存在此类键,则返回提到的默认值。

输出

......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) 函数返回移除的值。

输出

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

输出

......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 中不存在,它将添加新键并设置与该键对应的给定值。

输出

......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 中的所有键值对。

输出

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