hashMapOf()
是HashMap
类的函数。 它返回具有指定内容的新HashMap
。 它包含键和值形式的数据对。 HashMap
是可变集合,它提供了读写功能。
inline fun <K, V> hashMapOf(): HashMap<K, V> (source) // fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V> (source)
函数 | 描述 |
---|---|
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 中删除指定的键及其对应的值 |
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
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
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
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
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]}") } }
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 = 撸码
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]
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
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 = 撸代码
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 = 撸代码
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 = 撸代码
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 {}