个性化阅读
专注于IT技术分析

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(原始:Map <out K, V>) 它构造一个HashMap实例, 其中填充了指定原始地图的内容。

Kotlin HashMap类的功能

功能 描述
打开趣味看跌期权(键:K, 值:V):V? 它将指定的键和值放入地图中
打开运算符fun get(key:K):V? 它返回指定键的值;如果map中没有这样的指定键, 则返回null。
打开乐趣containsKey(key:K):布尔值 如果map包含指定键, 则返回true。
打开乐趣containsValue(value:V):布尔值 如果map将多个键之一映射到指定值, 则返回true。
open fun clear() 它从地图中删除所有元素。
打开乐趣删除(键:K):V? 它从映射中删除指定的键及其对应的值

Kotlin HashMap示例1-空HashMap

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

fun main(args: Array<String>){

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

Kotlin 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

Kotlin 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, "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

Kotlin 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, "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

Kotlin 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, "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().......
{}
赞(0)
未经允许不得转载:srcmini » Kotlin HashMap类

评论 抢沙发

评论前必须登录!