Kotlin 哈希图
Kotlin HashMap 是一个包含对象对的集合。基于 Kotlin 哈希表的 MutableMap 接口实现。它以键值对的形式存储数据。映射键是唯一的,并且映射只为每个键保存一个值。它表示为HashMap
基于哈希表的 HashMap 实现不保证集合的键、值和条目的指定数据的顺序。
Kotlin HashMap 类的构造函数——
Kotlin HashMap 提供了 4 个构造函数,每个的访问修饰符都是公共的:
HashMap 函数的使用——
Kotlin 程序使用函数 HashMap(), HashMap(original: Map), Traversing hashmap, HashMap.get() –
fun main(args: Array) {
//A simple example of HashMap class define
// with empty "HashMap of "
var hashMap : HashMap
= HashMap ()
//printing the Empty hashMap
printHashMap(hashMap)
//adding elements to the hashMap using
// put() function
hashMap.put("IronMan" , 3000)
hashMap.put("Thor" , 100)
hashMap.put("SpiderMan" , 1100)
hashMap.put("NickFury" , 1200)
hashMap.put("HawkEye" , 1300)
//printing the non-Empty hashMap
printHashMap(hashMap)
//using the overloaded print function of
//Kotlin language to get the same results
println("hashMap : " + hashMap + "\n")
//hashMap traversal using a for loop
for(key in hashMap.keys){
println("Element at key $key : ${hashMap[key]}")
}
//creating another hashMap object with
// the previous version of hashMap object
var secondHashMap : HashMap
= HashMap (hashMap)
println("\n" + "Second HashMap : ")
for(key in secondHashMap.keys){
//using hashMap.get() function to fetch the values
println("Element at key $key : ${hashMap.get(key)}")
}
//this will clear the whole map and make it empty
println("hashMap.clear()")
hashMap.clear()
println("After Clearing : " + hashMap)
}
//function to print the hashMap
fun printHashMap(hashMap : HashMap){
// isEmpty() function to check whether
// the hashMap is empty or not
if(hashMap.isEmpty()){
println("hashMap is empty")
}else{
println("hashMap : " + hashMap)
}
}
输出 :
hashMap is empty : {}
hashMap : {Thor=100, HawkEye=1300, NickFury=1200, IronMan=3000, SpiderMan=1100}
hashMap : {Thor=100, HawkEye=1300, NickFury=1200, IronMan=3000, SpiderMan=1100}
Element at key Thor : 100
Element at key HawkEye : 1300
Element at key NickFury : 1200
Element at key IronMan : 3000
Element at key SpiderMan : 1100
secondHashMap :
Element at key Thor : 100
Element at key HawkEye : 1300
Element at key IronMan : 3000
Element at key NickFury : 1200
Element at key SpiderMan : 1100
hashMap.clear()
After Clearing : {}
Kotlin 程序使用 HashMap 初始容量,HashMap.size –
fun main(args: Array) {
//HashMap can also be initialize
// with its initial capacity.
//The capacity can be changed by
// adding and replacing its element.
var hashMap : HashMap
= HashMap (4)
//adding elements to the hashMap using put() function
hashMap.put("IronMan" , 3000)
hashMap.put("Thor" , 100)
hashMap.put("SpiderMan" , 1100)
hashMap.put("NickFury" , 1200)
for(key in hashMap.keys) {
println("Element at key $key : ${hashMap[key]}")
}
//returns the size of hashMap
println("\n" + "hashMap.size : " + hashMap.size )
//adding a new element in the hashMap
hashMap["BlackWidow"] = 1000;
println("hashMap.size : " + hashMap.size + "\n")
for(key in hashMap.keys) {
println("Element at key $key : ${hashMap[key]}")
}
}
输出:
Element at key Thor : 100
Element at key IronMan : 3000
Element at key NickFury : 1200
Element at key SpiderMan : 1100
hashMap.size : 4
hashMap.size : 5
Element at key Thor : 100
Element at key BlackWidow : 1000
Element at key IronMan : 3000
Element at key NickFury : 1200
Element at key SpiderMan : 1100
Kotlin 程序使用函数 HashMap.get(key), HashMap.replace(), HashMap.put() –
fun main(args: Array) {
var hashMap : HashMap
= HashMap ()
//adding elements to the hashMap
// using put() function
hashMap.put("IronMan" , 3000)
hashMap.put("Thor" , 100)
hashMap.put("SpiderMan" , 1100)
hashMap.put("Cap" , 1200)
for(key in hashMap.keys) {
println("Element at key $key : ${hashMap[key]}")
}
//the hashMap's elements can be accessed like this
println("\nhashMap[\"IronMan\"] : "
+ hashMap["IronMan"])
hashMap["Thor"] = 2000
println("hashMap.get(\"Thor\") : "
+ hashMap.get("Thor") + "\n")
//replacing some values
hashMap.replace("Cap" , 999);
hashMap.put("Thor" , 2000);
println("hashMap.replace(\"Cap\" , 999)" +
" hashMap.replace(\"Thor\" , 2000)) :")
for(key in hashMap.keys) {
println("Element at key $key : ${hashMap[key]}")
}
}
输出:
Element at key Thor : 100
Element at key Cap : 1200
Element at key IronMan : 3000
Element at key SpiderMan : 1100
hashMap["IronMan"] : 3000
hashMap.get("Thor") : 2000
hashMap.replace("Cap", 999) hashMap.replace("Thor", 2000)) :
Element at key Thor : 2000
Element at key Cap : 999
Element at key IronMan : 3000
Element at key SpiderMan : 1100
HashMap 的时间复杂度——
Kotlin HashMap 为 get 和 put 等基本操作提供恒定的时间或 O(1) 复杂度,前提是哈希函数编写得当并且它正确地分散了元素。如果在 HashMap 中搜索 containsKey() 只是一个 get() 丢弃检索到的值,它是 O(1) (假设哈希函数正常工作)。
Kotlin HashMap 类的一些其他功能——
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。