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