Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Kotlin mutableSetOf() method

  • Last Updated : 15 Jul, 2021

Kotlin Set interface is a generic unordered collection of elements and it does not contain duplicate elements. Kotlin supports two types of sets mutable and immutable. 
setOf() is immutable means it supports only read-only functionalities and mutableSetOf() is mutable means it supports read and write both functionality. 

Syntax: 

fun <T> mutableSetOf( vararg elements: T): MutableSet<T>

Description:  

  • This function returns a set of given elements, which can be both read and written.
  • The returned set preserves the element iteration order.

Kotlin program of mutableSetOf() function :

Kotlin






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

Output: 

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

Adding and removing elements in a set –

We can add elements in a mutable set using the add() function, and remove an elements using remove () function.

Example :  

Kotlin




fun main(args: Array<String>)
{
    //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)
}

Output: 

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

Set Indexing –

Using index functions indexOf() , lastIndexOf() we can get the index of the specified element. And we can also find the elements at some specific index using elementAt() function.

Kotlin program of using index –  

Kotlin




fun main(args: Array<String>) {
 
    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"))
}

Output: 

The element at index 2 is: Root
The index of element is: 1
The last index of element is: 4

Set first and last element –

We can get the first and element of a set using first() and last() functions. 



Kotlin program –  

Kotlin




fun main(args: Array<String>){
    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())
}

Output: 

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

Traversal in a mutableSet –

We can run a for loop with an iterator which traverse all the items in the set . 

Kotlin




fun main(args: Array<String>)
{
    //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 )
}

Output: 

1
2
3
4

contains() and containsAll() functions –

Both the methods are used to check whether an element is present in the set or not? 

Kotlin program of using contains() and containsAll() function –  

Kotlin




fun main(args: Array<String>){
    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")))
}

Output: 

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

Checking equality of empty sets and use of isEmpty() functions –

fun <T> mutableSetOf(): mutableSet<T>

This syntax returns an empty set of specific type.

Kotlin program of using isEmpty() function –  

Kotlin




fun main(args: Array<String>) {
    //creating an empty set of strings
    val seta = mutableSetOf<String>()
    //creating an empty set of integers
    val setb = mutableSetOf<Int>()
  
  
    //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
}

Output : 

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

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!