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().......
{}
上一篇:
Kotlin Map介面
下一篇:
Kotlin HashMap:hashMapOf()函數
