Kotlin - hashSetOf()

Kotlin - hashSetOf() 首页 / Kotlin入门教程 / Kotlin - hashSetOf()

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

HashSet声明

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

示例1 - capacity

让无涯教程创建一个定义其容量的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

示例2 - generic

更具体地说,无涯教程可以使用其方法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

示例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

示例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

示例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

示例6 - isEmpty() and 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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

软件测试52讲 -〔茹炳晟〕

从0开始学大数据 -〔李智慧〕

OAuth 2.0实战课 -〔王新栋〕

用户体验设计实战课 -〔相辉〕

如何讲好一堂课 -〔薛雨〕

去无方向的信 -〔小麥〕

遗留系统现代化实战 -〔姚琪琳〕

B端产品经理入门课 -〔董小圣〕

结构思考力 · 透过结构看问题解决 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)