Kotlin - HashMap

Kotlin - HashMap 首页 / Kotlin入门教程 / Kotlin - HashMap

Kotlin HashMap是基于MutableMap接口的集合类。 Kotlin的HashMap类使用Hash表实现MutableMap接口。它以键和值对的形式存储数据。它表示为HashMap <key,value>或HashMap <K,V>。

HashMap类的实现不能保证键,值和集合条目的数据顺序。

HashMap构造方法

ConstructorDescription
HashMap()构造一个空的HashMap实例
HashMap(initialCapacity: Int, loadFactor: Float = 0f)构造指定容量的HashMap实例
HashMap(original: Map<out K, V>)构造一个HashMap实例,填充了指定Map的内容。

Kotlin Hashmap类的函数

FunctionsDescription
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中删除指定的键及其对应的值

HashMap示例1

让无涯教程创建一个简单的HashMap类示例示例,该示例使用<Int,String>的空HashMap定义并稍后添加元素。要打印HashMap的值,无涯教程将使用HashMap [key]或HashMap.get(key)。

fun main(args: Array<String>){

    val hashMap:HashMap<Int,String> = HashMap<Int,String>() //定义空哈希
    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示例2- HashMap初始容量

HashMap还可以以初始容量初始化。可以通过添加和替换其元素来更改容量。

fun main(args: Array<String>){

    val hashMap:HashMap<String,String> = HashMap<String,String>(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

HashMap示例3-remove() 和 put()

函数remove()用于将指定键处的现有值替换为指定值。 put()函数在指定的键处添加一个新值,并替换旧值。如果put()函数未找到任何指定的键,它将在指定的键处放置一个新值。

fun main(args: Array<String>){

    val hashMap:HashMap<Int,String> = HashMap<Int,String>()
    hashMap.put(1,"Ajay")
    hashMap.put(3,"Vijay")
    hashMap.put(4,"Learnfk")
    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 = Learnfk
.....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 = Learnfk

HashMap示例4 - containsKey(key)和containsValue(value)

如果HashMap中存在指定的键,则函数containsKey()返回true;如果不存在此键,则返回false。

函数containsValue()用于检查HashMap中是否存在指定的值。如果HashMap中存在value,它将返回true,否则返回false。

fun main(args: Array<String>){

    val hashMap:HashMap<Int,String> = HashMap<Int,String>()
    hashMap.put(1,"Ajay")
    hashMap.put(3,"Vijay")
    hashMap.put(4,"Learnfk")
    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 = Learnfk
.....hashMap.containsKey(3).......
true
.....hashMap.containsValue("Rohan").......
true

HashMap示例5 - clear()

clear()函数用于清除HashMap的所有数据。

fun main(args: Array<String>){

    val hashMap:HashMap<Int,String> = HashMap<Int,String>()
    hashMap.put(1,"Ajay")
    hashMap.put(3,"Vijay")
    hashMap.put(4,"Learnfk")
    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 = Learnfk
.....hashMap.clear().......
.....print hashMap after clear().......
{}

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

ZooKeeper实战与源码剖析 -〔么敬国〕

NLP实战高手课 -〔王然〕

大数据经典论文解读 -〔徐文浩〕

说透低代码 -〔陈旭〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

林外 · 专利写作第一课 -〔林外〕

说透元宇宙 -〔方军〕

Kubernetes入门实战课 -〔罗剑锋〕

Vue 3 企业级项目实战课 -〔杨文坚〕

好记忆不如烂笔头。留下您的足迹吧 :)