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().......
{}

上一篇:Kotlin Map接口

下一篇:Kotlin HashMap:hashMapOf()函数

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程