📜  Kotlin mutableSetOf() 方法

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

Kotlin mutableSetOf() 方法

Kotlin Set 接口是一个通用的无序元素集合,它不包含重复的元素。 Kotlin 支持两种类型的集合可变和不可变。
setOf()不可变的,意味着它只支持只读功能,而mutableSetOf()可变的,意味着它支持读取和写入这两种功能。

句法:

fun  mutableSetOf( vararg elements: T): MutableSet

描述:

  • 此函数返回一组给定的元素,这些元素既可以读取也可以写入。
  • 返回的集合保留元素迭代顺序。

mutableSetOf()函数的 Kotlin 程序

Kotlin
fun main(args: Array)
{
    //declaring a mutable set of integers
    val mutableSetA = mutableSetOf( 1 , 2 , 3 , 4 , 3);
    println(mutableSetA)
 
    //declaring a mutable set of strings
    val mutableSetB = mutableSetOf("Geeks","for" , "geeks");
    println(mutableSetB)
 
    //declaring an empty mutable set of integers
    val mutableSetC = mutableSetOf()
    println(mutableSetC)
}


Kotlin
fun main(args: Array)
{
    //declaring a mutable set of integers
    val seta = mutableSetOf( 1 , 2 , 3 , 4 , 3);
    println(seta);
    //adding elements 6 & 7
    seta.add(6);
    seta.add(7);
    println(seta);
      
    //removing 3 from the set
    seta.remove(3);
    println(seta);
      
    //another way to add elements is by using listOf() function
    seta += listOf(8,9)
    println(seta)
}


Kotlin
fun main(args: Array) {
 
    val captains = mutableSetOf("Kohli","Smith","Root","Malinga","Rohit","Dhawan")
 
    println("The element at index 2 is: "+captains.elementAt(2))
 
    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 = mutableSetOf(1,2,3,4,"Kohli","Smith",
        "Root","Malinga","Dhawan","Rohit")
 
    println("The first element of the set is: "+captains.first())
 
    println("The last element of the set is: "+captains.last())
}


Kotlin
fun main(args: Array)
{
    //declaring a mutable set of integers
    val seta = mutableSetOf( 1 , 2 , 3 , 4 , 3);
  
    //traversal of seta using an iterator 'item'
    for(item in seta)
        println( item )
}


Kotlin
fun main(args: Array){
    val captains = mutableSetOf(1,2,3,4,"Kohli","Smith",
        "Root","Malinga","Rohit","Dhawan")
   
   
    var name = "Dhawan"
    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,"Root")))
}


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


输出:

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

在集合中添加和删除元素 –

我们可以使用add()函数在可变集合添加元素,并使用remove ()函数删除元素。

例子 :

科特林

fun main(args: Array)
{
    //declaring a mutable set of integers
    val seta = mutableSetOf( 1 , 2 , 3 , 4 , 3);
    println(seta);
    //adding elements 6 & 7
    seta.add(6);
    seta.add(7);
    println(seta);
      
    //removing 3 from the set
    seta.remove(3);
    println(seta);
      
    //another way to add elements is by using listOf() function
    seta += listOf(8,9)
    println(seta)
}

输出:

[1, 2, 3, 4]
[1, 2, 3, 4, 6, 7]
[1, 2, 4, 6, 7]
[1, 2, 4, 6, 7, 8, 9]

设置索引 –

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

Kotlin 使用索引的程序——

科特林

fun main(args: Array) {
 
    val captains = mutableSetOf("Kohli","Smith","Root","Malinga","Rohit","Dhawan")
 
    println("The element at index 2 is: "+captains.elementAt(2))
 
    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: Root
The index of element is: 1
The last index of element is: 4

设置第一个和最后一个元素 -

我们可以使用 first() 和 last() 函数获取集合的第一个和元素。

Kotlin 程序 –

科特林

fun main(args: Array){
    val captains = mutableSetOf(1,2,3,4,"Kohli","Smith",
        "Root","Malinga","Dhawan","Rohit")
 
    println("The first element of the set is: "+captains.first())
 
    println("The last element of the set is: "+captains.last())
}

输出:

The first element of the set is: 1
The last element of the set is: Dhawan 

mutableSet 中的遍历 –

我们可以运行一个带有迭代器的 for 循环,它遍历 set 中的所有项目。

科特林

fun main(args: Array)
{
    //declaring a mutable set of integers
    val seta = mutableSetOf( 1 , 2 , 3 , 4 , 3);
  
    //traversal of seta using an iterator 'item'
    for(item in seta)
        println( item )
}

输出:

1
2
3
4

contains() 和 containsAll() 函数 –

这两种方法都用于检查集合中是否存在元素?

使用 contains() 和 containsAll()函数的 Kotlin 程序 -

科特林

fun main(args: Array){
    val captains = mutableSetOf(1,2,3,4,"Kohli","Smith",
        "Root","Malinga","Rohit","Dhawan")
   
   
    var name = "Dhawan"
    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,"Root")))
}

输出:

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

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

fun  mutableSetOf(): mutableSet

此语法返回特定类型的空集。

使用 isEmpty()函数的 Kotlin 程序 –

科特林

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

输出 :

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