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

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(元素:集合<E>) 它使用指定集合的​​元素构造一个HashSet实例。

Kotlin HashSet类的功能

功能 描述
open fun add(e:E):布尔值 它将给定元素添加到集合中。
打开运算符fun contains(e:E):布尔值 它检查指定的元素是否存在于当前集合中。
打开乐趣isEmpty():布尔值 它检查当前集合为空(不包含任何元素)。如果找到的集合为空, 则返回true, 否则返回false。
打开有趣的iterator():MutableIterator <E> 它在当前对象的元素上返回一个迭代器。
打开乐趣remove(e:E):布尔值 如果当前集合中存在提及元素, 它将删除提及元素。如果删除了否则返回false。
open fun clear() 它将删除此集合中的所有元素。

Kotlin HashSet类的属性

属性 描述
开阀尺寸:整数 此属性用于返回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-通用

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

fun main(args: Array<String>){
    var hashSetOf1 = hashSetOf<Int>(2, 13, 6, 5, 2, 8)
    var hashSetOf2: HashSet<String> = hashSetOf<String>("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<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中存在的元素总数。如果其中的提及元素包含在collection中, 则contains()函数返回true;而containsAll()函数检查指定collection中的所有元素是否包含在此collection中。

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
赞(0)
未经允许不得转载:srcmini » Kotlin HashSet类

评论 抢沙发

评论前必须登录!