Kotlin HashSet類


Kotlin HashSet是一個集合類,它擴充套件了AbstractMutableSet類並實現了Set介面。 HashSet類使用雜湊機制儲存元素。 它支援讀寫功能。 但它不支援重複值,也不保證元素的順序。

HashSet類的宣告

open class HashSet<E> : AbstractMutableSet<E> (source)

Kotlin HashSet類別建構函式

建構函式 描述
HashSet() 它構造一個空的HashSet範例
HashSet(initialCapacity: Int, loadFactor: Float = 0f) 它用於構造指定容量的HashSet
HashSet(elements: Collection<E>) 它使用指定集合的元素構造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<E> 它返回當前物件元素的疊代器。
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<String>){
    var hashSet = HashSet<Int>(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 - 泛型

為了更具體,可以使用HashSet類的hashSetOf <T>()方法提供泛型型別支援。

fun main(args: Array<String>){
    var hashSetOf1 = hashSetOf<Int>(2,13,6,5,2,8)
    var hashSetOf2: HashSet<String> = hashSetOf<String>("Python","Ajax" ,"Python","Ruby")
    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......
Ruby
Python
Ajax

Kotlin HashSet範例3 - add() 和 addAll()函式

add()函式用於在HashSet範例中新增元素,而addAll()函式將指定集合的所有元素新增到HashSet

fun main(args: Array<String>){
    var hashSet = HashSet<Int>(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中存在的總元素。 如果contains()函式中的指定元素包含在集合中,則contains()函式返回true,而containsAll()函式檢查此集合中是否包含指定集合的所有元素。

fun main(args: Array<String>){
    var hashSetOf1: HashSet<Int> = hashSetOf<Int>(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<String>){
    var hashSetOf1: HashSet<Int> = hashSetOf<Int>(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<String>){
    var hashSetOf1: HashSet<Int> = hashSetOf<Int>(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