📅  最后修改于: 2021-01-05 07:47:19             🧑  作者: Mango
Kotlin HashMap是基于MutableMap接口的集合类。 Kotlin HashMap类使用Hash表实现MutableMap接口。它以键和值对的形式存储数据。它表示为HashMap
HashMap类的实现不能保证键,值和集合条目的数据顺序。
Constructor | Description |
---|---|
HashMap() | It constructs an empty HashMap instance |
HashMap(initialCapacity: Int, loadFactor: Float = 0f) | It is used to constructs a HashMap of specified capacity. |
HashMap(original: Map |
It constructs a HashMap instance filled with contents of specified original map. |
Functions | Description |
---|---|
open fun put(key: K, value: V): V? | It puts the specified key and value in the map |
open operator fun get(key: K): V? | It returns the value of specified key, or null if no such specified key is available in map. |
open fun containsKey(key: K): Boolean | It returns true if map contains specifies key. |
open fun containsValue(value: V): Boolean | It returns true if map maps one of more keys to specified value. |
open fun clear() | It removes all elements from map. |
open fun remove(key: K): V? | It removes the specified key and its corresponding value from map |
让我们创建一个简单的HashMap类定义示例,该类使用
fun main(args: Array){
val hashMap:HashMap = HashMap() //define empty hashmap
hashMap.put(1,"Ajay")
hashMap.put(3,"Vijay")
hashMap.put(4,"Praveen")
hashMap.put(2,"Ajay")
println(".....traversing hashmap.......")
for(key in hashMap.keys){
println("Element at key $key = ${hashMap[key]}")
}}
输出:
.....traversing hashmap.......
Element at key 1 = Ajay
Element at key 2 = Ajay
Element at key 3 = Vijay
Element at key 4 = Praveen
HashMap也可以使用其初始容量进行初始化。可以通过添加和替换其元素来更改容量。
fun main(args: Array){
val hashMap:HashMap = HashMap(3)
hashMap.put("name","Ajay")
hashMap.put("city","Delhi")
hashMap.put("department","Software Development")
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 = Ajay
Element at key department = Software Development
Element at key city = Delhi
.....hashMap.size.......
3
.....hashMap.size after adding hobby.......
4
.....traversing hashmap.......
Element at key name = Ajay
Element at key department = Software Development
Element at key city = Delhi
Element at key hobby = Travelling
函数remove()用于将指定键处的现有值替换为指定值。 put()函数在指定的键处添加一个新值,并替换旧值。如果put()函数未找到任何指定的键,它将在指定的键处放置一个新值。
fun main(args: Array){
val hashMap:HashMap = HashMap()
hashMap.put(1,"Ajay")
hashMap.put(3,"Vijay")
hashMap.put(4,"Prakash")
hashMap.put(2,"Rohan")
println(".....traversing hashmap.......")
for(key in hashMap.keys){
println("Element at key $key = ${hashMap[key]}")
}
hashMap.replace(3,"Ashu")
hashMap.put(2,"Raj")
println(".....hashMap.replace(3,\"Ashu\")... hashMap.replace(2,\"Raj\").......")....")
for(key in hashMap.keys){
println("Element at key $key = ${hashMap[key]}")
}
}
输出:
.....traversing hashmap.......
Element at key 1 = Ajay
Element at key 2 = Rohan
Element at key 3 = Vijay
Element at key 4 = Prakash
.....hashMap.replace(3,"Ashu")...hashMap.put(2,"Raj")....
Element at key 1 = Ajay
Element at key 2 = Raj
Element at key 3 = Ashu
Element at key 4 = Prakash
如果HashMap中存在指定的键,则函数containsKey()返回true;如果不存在这样的键,则返回false。
函数containsValue()用于检查HashMap中是否存在指定的值。如果HashMap中存在value,它将返回true,否则返回false。
fun main(args: Array){
val hashMap:HashMap = HashMap()
hashMap.put(1,"Ajay")
hashMap.put(3,"Vijay")
hashMap.put(4,"Prakash")
hashMap.put(2,"Rohan")
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(\"Rohan\").......")
println(hashMap.containsValue("Rohan"))
}
输出:
.....traversing hashmap.......
Element at key 1 = Ajay
Element at key 2 = Rohan
Element at key 3 = Vijay
Element at key 4 = Prakash
.....hashMap.containsKey(3).......
true
.....hashMap.containsValue("Rohan").......
true
clear()函数用于清除HashMap中的所有数据。
fun main(args: Array){
val hashMap:HashMap = HashMap()
hashMap.put(1,"Ajay")
hashMap.put(3,"Vijay")
hashMap.put(4,"Prakash")
hashMap.put(2,"Rohan")
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 = Ajay
Element at key 2 = Rohan
Element at key 3 = Vijay
Element at key 4 = Prakash
.....hashMap.clear().......
.....print hashMap after clear().......
{}