Kotlin HashMap類


Kotlin HashMap是基於MutableMap介面的集合類。 Kotlin HashMap類使用Hash表實現MutableMap介面。 它以鍵和值對的形式儲存資料。 它表示為HashMap <key,value>HashMap <K,V>

HashMap類的實現不保證鍵,值和集合資料專案的順序。

Kotlin HashMap類別建構函式

造函式 描述
HashMap() 它構造一個空的HashMap範例
HashMap(initialCapacity: Int, loadFactor: Float = 0f) 它用於構造指定容量的HashMap
HashMap(original: Map<out K, V>) 它構造一個使用指定原始Map內容填充的HashMap範例。

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 HashMap範例1 - 空HashMap

下面是使用<Int,String>的空HashMap建立一個簡單的HashMap類定義範例,之後再新增元素。可使用HashMap [key]HashMap.get(key)列印HashMap的值。

fun main(args: Array<String>){

    val hashMap:HashMap<Int,String> = HashMap<Int,String>() //define empty hashmap
    hashMap.put(1,"Java")
    hashMap.put(3,"SEO")
    hashMap.put(4,"Python")
    hashMap.put(2,"Kotlin")
    println(".....traversing hashmap.......")
    for(key in hashMap.keys){
        println("Element at key $key = ${hashMap[key]}")
    }
}

執行上面範例程式碼,得到以下結果 -

.....traversing hashmap.......
Element at key 1 = Java
Element at key 2 = Kotlin
Element at key 3 = SEO
Element at key 4 = Python

Kotlin HashMap範例2- HashMap初始容量

HashMap也可以使用初始容量進行初始化。 可以通過新增和替換元素來更改容量。

fun main(args: Array<String>){

    val hashMap:HashMap<String,String> = HashMap<String,String>(3)
    hashMap.put("name","Maxsu")
    hashMap.put("city","海口")
    hashMap.put("department","技術部")
    println(".....traversing hashmap.......")
    for(key in hashMap.keys){
        println("Element at key: $key => ${hashMap[key]}")
    }
    println(".....hashMap.size.......")
    println(hashMap.size)
    hashMap.put("hobby","Travelling")
    println(".....hashMap.size  after adding hobby.......")
    println(hashMap.size)
    println(".....traversing hashmap.......")
    for(key in hashMap.keys){
        println("Element at key: $key => ${hashMap.get(key)}")
    }
}

執行上面範例程式碼,得到以下結果 -

.....traversing hashmap.......
Element at key: name => Maxsu
Element at key: department => 技術部
Element at key: city => 海口
.....hashMap.size.......
3
.....hashMap.size  after adding hobby.......
4
.....traversing hashmap.......
Element at key: name => Maxsu
Element at key: department => 技術部
Element at key: city => 海口
Element at key: hobby => Travelling

Kotlin HashMap範例3- remove()和put()函式

remove()函式用於將指定鍵的現有值替換為指定值。 put()函式在指定鍵處新增新值並替換舊值。 如果put()函式沒有找到指定的鍵,它會在指定的鍵上放置一個新值。

fun main(args: Array<String>){

    val hashMap:HashMap<Int,String> = HashMap<Int,String>()
    hashMap.put(1,"Python")
    hashMap.put(3,"Java")
    hashMap.put(4,"Kotlin")
    hashMap.put(2,"Ruby")

    println(".....traversing hashmap.......")
    for(key in hashMap.keys){
        println("Element at key: $key => ${hashMap[key]}")
    }

    hashMap.replace(3,"Susen")
    hashMap.put(2,"Maxsu")
    println(".....hashMap.replace(3,"Susen")... hashMap.replace(2,"Maxsu")........")
    for(key in hashMap.keys){
        println("Element at key: $key => ${hashMap[key]}")
    }
}

執行上面範例程式碼,得到以下結果 -

.....traversing hashmap.......
Element at key: 1 => Python
Element at key: 2 => Ruby
Element at key: 3 => Java
Element at key: 4 => Kotlin
.....hashMap.replace(3,"Susen")... hashMap.replace(2,"Maxsu")........
Element at key: 1 => Python
Element at key: 2 => Maxsu
Element at key: 3 => Susen
Element at key: 4 => Kotlin

Kotlin HashMap範例4 - containsKey(key)和containsValue(value)函式

如果指定的鍵在HashMap中存在,則containsKey()函式返回true;如果不存在此鍵,則返回false

containsValue()函式用於檢查HashMap中是否存在指定的值。 如果HashMap中存在指定值,則返回true,否則返回false

fun main(args: Array<String>){

    val hashMap:HashMap<Int,String> = HashMap<Int,String>()
    hashMap.put(1,"Ruby")
    hashMap.put(3,"Kotlin")
    hashMap.put(4,"Java")
    hashMap.put(2,"Python")

    println(".....traversing hashmap.......")
    for(key in hashMap.keys){
        println("Element at key: $key => ${hashMap[key]}")
    }

    println(".....hashMap.containsKey(3).......")
    println(hashMap.containsKey(3))
    println(".....hashMap.containsValue("Swift").......")
    println(hashMap.containsValue("Swift"))
}

執行上面範例程式碼,得到以下結果 -

.....traversing hashmap.......
Element at key: 1 => Ruby
Element at key: 2 => Python
Element at key: 3 => Kotlin
Element at key: 4 => Java
.....hashMap.containsKey(3).......
true
.....hashMap.containsValue("Swift").......
false

Kotlin HashMap範例5 - clear()函式

clear()函式用於清除HashMap中的所有資料。如下範例 -

fun main(args: Array<String>){

    val hashMap:HashMap<Int,String> = HashMap<Int,String>()
    hashMap.put(1,"Java")
    hashMap.put(3,"Python")
    hashMap.put(4,"Kotlin")
    hashMap.put(2,"Ruby")

    println(".....traversing hashmap.......")
    for(key in hashMap.keys){
        println("Element at key: $key => ${hashMap[key]}")
    }

    println(".....hashMap.clear().......")
    hashMap.clear()
    println(".....print hashMap after clear().......")
    println(hashMap)
}

執行上面範例程式碼,得到以下結果 -

.....traversing hashmap.......
Element at key: 1 => Java
Element at key: 2 => Ruby
Element at key: 3 => Python
Element at key: 4 => Kotlin
.....hashMap.clear().......
.....print hashMap after clear().......
{}