Kotlin教程:HashSet类(使用散列机制存储元素)

2023年 7月 12日 21.7k 0

Kotlin HashSet是一个集合类,它扩展了AbstractMutableSet类并实现了Set接口。 HashSet类使用散列机制存储元素。 它支持读写功能。 但它不支持重复值,也不保证元素的顺序。

HashSet类的声明

open class HashSet : AbstractMutableSet (source)

Kotlin

Kotlin HashSet类的构造函数

构造函数 描述
HashSet() 它构造一个空的HashSet实例
HashSet(initialCapacity: Int, loadFactor: Float = 0f) 它用于构造指定容量的HashSet
HashSet(elements: Collection) 它使用指定集合的元素构造HashSet实例。

Kotlin HashSet类的函数

函数 描述
open fun add(element: E): Boolean 它将给定元素添加到集合中。
open operator fun contains(element: E): Boolean 它检查当前集合中是否存在指定的元素。
open fun isEmpty(): Boolean 它检查当前集合是否为空(不包含任何元素)。 如果找到的集合为空则返回true,否则返回false
open fun iterator(): MutableIterator 它返回当前对象元素的迭代器。
open fun remove(element: E): Boolean 如果当前集合中存在,它将删除提及元素。 如果删除成功则返回true,则返回false
open fun clear() 它会删除此集合中的所有元素。

Kotlin HashSet的属性

函数 描述
open val size: Int 此属性用于返回HashSet集合的大小。

Kotlin HashSet示例1 - 容量

下面是一个定义容量的HashSet示例。 容量是指要在HashSet中添加的元素总数。 根据需要可以稍后增加减少量。

fun main(args: Array){
    var hashSet = HashSet(6)
    hashSet.add(2)
    hashSet.add(13)
    hashSet.add(6)
    hashSet.add(5)
    hashSet.add(2)
    hashSet.add(8)
    println("......traversing hashSet......")
    for (element in hashSet){
        println(element)
    }
}

Kotlin

执行上面示例代码,得到以下结果 -

......traversing hashSet......
8
2
13
5
6

Shell

Kotlin HashSet示例2 - 泛型

为了更具体,可以使用HashSet类的hashSetOf ()方法提供泛型类型支持。

fun main(args: Array){
    var hashSetOf1 = hashSetOf(2,13,6,5,2,8)
    var hashSetOf2: HashSet = hashSetOf("Python","Ajax" ,"Python","Ruby")
    println("......traversing hashSetOf1......")
    for (element in hashSetOf1){
        println(element)
    }
    println("......traversing hashSetOf2......")
    for (element in hashSetOf2){
        println(element)
    }
}

Kotlin

执行上面示例代码,得到以下结果 -

......traversing hashSetOf1......
8
2
13
5
6
......traversing hashSetOf2......
Ruby
Python
Ajax

Shell

Kotlin HashSet示例3 - add() 和 addAll()函数

add()函数用于在HashSet实例中添加元素,而addAll()函数将指定集合的所有元素添加到HashSet

fun main(args: Array){
    var hashSet = HashSet(3)
    val intSet = setOf(6,4,29)
    hashSet.add(2)
    hashSet.add(13)
    hashSet.add(6)
    hashSet.add(5)
    hashSet.add(2)
    hashSet.add(8)
    println("......traversing hashSet......")
    for (element in hashSet){
        println(element)
    }
    hashSet.addAll(intSet)
    println("......traversing hashSet after hashSet.addAll(intSet)......")
    for (element in hashSet){
        println(element)
    }
}

Kotlin

执行上面示例代码,得到以下结果 -

......traversing hashSet......
8
2
13
5
6
......traversing hashSet after hashSet.addAll(intSet)......
2
4
5
6
8
13
29

Shell

Kotlin HashSet示例4 - size, contains() 和 containsAll()函数

size属性返回HashMap中存在的总元素。 如果contains()函数中的指定元素包含在集合中,则contains()函数返回true,而containsAll()函数检查此集合中是否包含指定集合的所有元素。

fun main(args: Array){
    var hashSetOf1: HashSet = hashSetOf(2,6,13,4,29,15)
    val mySet = setOf(6,4,29)

    println("......traversing hashSetOf1......")
    for (element in hashSetOf1){
        println(element)
    }
    println(".....hashSetOf1.size.....")
    println(hashSetOf1.size)
    println(".....hashSetOf1.contains(13).....")
    println(hashSetOf1.contains(13))
    println("....hashSetOf1.containsAll(mySet)...")
    println(hashSetOf1.containsAll(mySet))
}

Kotlin

执行上面示例代码,得到以下结果 -

......traversing hashSetOf1......
2
4
13
29
6
15
.....hashSetOf1.size.....
6
.....hashSetOf1.contains(13).....
true
....hashSetOf1.containsAll(mySet)...
true

Shell

Kotlin HashSet示例5 - remove() 和 removeAll()函数

remove()函数从集合中删除指定的元素(如果存在),而removeAll()函数从当前集合中删除所有指定的元素(如果存在)。

fun main(args: Array){
    var hashSetOf1: HashSet = hashSetOf(2,6,13,4,29,15)
    val mySet = setOf(6,4,29)

    println("......traversing hashSetOf1......")
    for (element in hashSetOf1){
        println(element)
    }
    println(".....hashSetOf1.remove(6)......")
    println(hashSetOf1.remove(6))
    println("......traversing hashSetOf1 after remove(6)......")
    for (element in hashSetOf1){
        println(element)
    }
    println("......hashSetOf1.removeAll(mySet)......")
    println(hashSetOf1.removeAll(mySet))
    println("......traversing hashSetOf1 after removeAll(mySet)......")
    for (element in hashSetOf1){
        println(element)
    }
}

Kotlin

执行上面示例代码,得到以下结果 -

......traversing hashSetOf1......
2
4
13
29
6
15
.....hashSetOf1.remove(6)......
true
......traversing hashSetOf1 after remove(6)......
2
4
13
29
15
......hashSetOf1.removeAll(mySet)......
true
......traversing hashSetOf1 after removeAll(mySet)......
2
13
15

Shell

Kotlin HashSet示例6 - isEmpty() 和 isNotEmpty()函数

isEmpty()函数检查当前集合是否为空,而isNotEmpty()函数检查当前集合是否不为空。

fun main(args: Array){
    var hashSetOf1: HashSet = hashSetOf(2,6,13,4,29,15)

    println("......traversing hashSetOf1......")
    for (element in hashSetOf1){
        println(element)
    }
    println(".....hashSetOf1.isEmpty()....")
    if(hashSetOf1.isEmpty()){
        println("hash set is empty")
    }
    else{
        println("hash set is not empty")
    }
    println(".....hashSetOf1.isNotEmpty()....")
    if(hashSetOf1.isNotEmpty()){
        println("hash set is not empty")
    }
    else{
        println("hash set is empty")
    }
}

Kotlin

执行上面示例代码,得到以下结果 -

......traversing hashSetOf1......
2
4
13
29
6
15
.....hashSetOf1.isEmpty()....
hash set is not empty
.....hashSetOf1.isNotEmpty()....
hash set is not empty

//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/kotlin/kotlin-hashset.html#article-start

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论