📜  Kotlin HashSet:hashSetOf()

📅  最后修改于: 2021-01-05 07:53:18             🧑  作者: Mango

Kotlin HashSet类

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

HashSet类声明

open class HashSet : AbstractMutableSet (source)

Kotlin HashSet类的构造方法

Constructor Description
HashSet() It constructs an empty HashSet instance
HashSet(initialCapacity: Int, loadFactor: Float = 0f) It is used to constructs a HashSet of specified capacity.
HashSet(elements: Collection) It constructs a HashSet instance using elements of specified collection.

Kotlin HashSet类的功能

Functions Description
open fun add(element: E): Boolean It adds the given element to the collection.
open operator fun contains(element: E): Boolean It checks the specified element is present in current collection.
open fun isEmpty(): Boolean It checks the current collection is empty (not contain any element). If found collection is empty returns true otherwise false.
open fun iterator(): MutableIterator It returns an iterator over the elements of current object.
open fun remove(element: E): Boolean It removes the mention element if present in current collection. It returns true if it removes otherwise false.
open fun clear() It deletes all the elements from this collection.

Kotlin HashSet类的属性

Property Description
open val size: Int This property is used to return the size of HashSet collection.

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)
    }    
}

输出:

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

Kotlin HashSet示例2-通用

更具体地说,我们可以使用其方法hashSetOf ()提供HashSet类的泛型类型。

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

输出:

......traversing hashSetOf1......
8
2
13
5
6
......traversing hashSetOf2......
Ashu
Roshan
Vijay

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)
    }
}

输出:

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

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

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

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))
}

输出:

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

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)
    }
}

输出:

......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

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")
    }
}

输出:

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