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