Kotlin HashSet是集合的类,它扩展了AbstractMutableSet类并实现Set接口。 HashSet类使用哈希机制存储元素。它同时支持读写函数。它不支持重复值,也不保证元素的顺序。
open class HashSet<E> : AbstractMutableSet<E> (source)
让无涯教程创建一个定义其容量的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
更具体地说,无涯教程可以使用其方法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
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
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
如果存在,则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
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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)