📜  Kotlin hashSetOf()

📅  最后修改于: 2022-05-13 01:54:36.259000             🧑  作者: Mango

Kotlin hashSetOf()

Kotlin HashSet 是一个通用的无序元素集合,它不包含重复的元素。它实现了set接口。 hashSetOf () 是一个函数,它返回一个可变的 hashSet,它可以被读取和写入。 HashSet 类使用散列机制存储所有元素。

句法:

fun  hashSetOf(vararg elements: T): HashSet

它返回具有给定元素的新 HashSet,但不保证存储时指定的顺序。

hashSetOf() 示例

Kotlin
fun main(args: Array)
{
  
    //declaring a hash set of integers
    val seta = hashSetOf(1,2,3,3);
    //printing first set
    println(seta)
      
    //declaring a hash set of strings
    val setb = hashSetOf("Geeks","for","geeks");
    println(setb);
      
}


Kotlin
fun main(args: Array)
{
    //declaring a hash set of integers
    val seta = hashSetOf();
    println(seta)
  
    //adding elements
    seta.add(1)
    seta.add(2)
  
    //making an extra set to add it in seta
    val newset = setOf(4,5,6)
    seta.addAll(newset)
  
    println(seta)
  
    //removing 2 from the set
    seta.remove(2)
    println(seta)    
}


Kotlin
fun main(args: Array)
{
    //declaring a hash set of integers
    val seta = hashSetOf(1,2,3,5);
      
    //traversing in a set using a for loop
    for(item in seta)
        println(item)
}


Kotlin
fun main(args: Array) {
 
    val captains = hashSetOf("Kohli","Smith","Root","Malinga","Rohit","Dhawan")
 
    println("The element at index 2 is: "+captains.elementAt(3))
 
    println("The index of element is: "+captains.indexOf("Smith"))
 
    println("The last index of element is: "+captains.lastIndexOf("Rohit"))
}


Kotlin
fun main(args: Array){
    val captains = hashSetOf(1,2,3,4,"Kohli","Smith",
        "Root","Malinga","Rohit","Dhawan")
 
 
    var name = "Rohit"
    println("The set contains the element $name or not?" +
            "   "+captains.contains(name))
 
    var num = 5
    println("The set contains the element $num or not?" +
            "   "+captains.contains(num))
 
    println("The set contains the given elements or not?" +
            "   "+captains.containsAll(setOf(1,3,"Dhawan","Warner")))
}


Kotlin
fun main(args: Array) {
    //creating an empty hash set of strings
    val seta = hashSetOf()
    //creating an empty hashset of integers
    val setb =hashSetOf()
 
 
    //checking if set is empty or not
    println("seta.isEmpty() is ${seta.isEmpty()}")
 
    // Since Empty hashsets are equal
 
    //checking if two hash sets are equal or not
    println("seta == setb is ${seta == setb}")
}


输出:

[1, 2, 3]
[Geeks, for, geeks] 

在 hashset 中添加和删除元素 –

  • 我们可以使用add () 和addAll () 函数在哈希集中添加元素。
  • 我们可以使用remove ()函数删除一个元素。

使用 add() 和 remove() 方法的 Kotlin 程序 –

科特林

fun main(args: Array)
{
    //declaring a hash set of integers
    val seta = hashSetOf();
    println(seta)
  
    //adding elements
    seta.add(1)
    seta.add(2)
  
    //making an extra set to add it in seta
    val newset = setOf(4,5,6)
    seta.addAll(newset)
  
    println(seta)
  
    //removing 2 from the set
    seta.remove(2)
    println(seta)    
}

输出:

[]
[1, 2, 4, 5, 6]
[1, 4, 5, 6] 

hashSet中的遍历-

我们可以在循环中使用迭代器遍历 hashSet。

科特林

fun main(args: Array)
{
    //declaring a hash set of integers
    val seta = hashSetOf(1,2,3,5);
      
    //traversing in a set using a for loop
    for(item in seta)
        println(item)
}

输出:

1
2
3
5

HashSet 索引 –

使用索引函数 indexOf() , lastIndexOf() 我们可以获得指定元素的索引。我们还可以使用 elementAt()函数找到某个特定索引处的元素。

Kotlin 使用索引的程序——

科特林

fun main(args: Array) {
 
    val captains = hashSetOf("Kohli","Smith","Root","Malinga","Rohit","Dhawan")
 
    println("The element at index 2 is: "+captains.elementAt(3))
 
    println("The index of element is: "+captains.indexOf("Smith"))
 
    println("The last index of element is: "+captains.lastIndexOf("Rohit"))
}

输出:

The element at index 2 is: Malinga
The index of element is: 4
The last index of element is: 0 

contains() 和 containsAll() 函数 –

这两种方法都用于检查 Hashset 中是否存在元素?
使用 contains() 和 containsAll()函数的 Kotlin 程序 -

科特林

fun main(args: Array){
    val captains = hashSetOf(1,2,3,4,"Kohli","Smith",
        "Root","Malinga","Rohit","Dhawan")
 
 
    var name = "Rohit"
    println("The set contains the element $name or not?" +
            "   "+captains.contains(name))
 
    var num = 5
    println("The set contains the element $num or not?" +
            "   "+captains.contains(num))
 
    println("The set contains the given elements or not?" +
            "   "+captains.containsAll(setOf(1,3,"Dhawan","Warner")))
}

输出:

The set contains the element Rohit or not?   true
The set contains the element 5 or not?   false
The set contains the given elements or not?   false 

检查空哈希集的相等性和 isEmpty() 函数的使用 -

fun  hashSetOf(): hashSet

此语法返回特定类型的空哈希集。
使用 isEmpty()函数的 Kotlin 程序 –

科特林

fun main(args: Array) {
    //creating an empty hash set of strings
    val seta = hashSetOf()
    //creating an empty hashset of integers
    val setb =hashSetOf()
 
 
    //checking if set is empty or not
    println("seta.isEmpty() is ${seta.isEmpty()}")
 
    // Since Empty hashsets are equal
 
    //checking if two hash sets are equal or not
    println("seta == setb is ${seta == setb}")
}

输出 :

seta.isEmpty() is true
seta == setb is true