Industrial Training




Kotlin MutableSet Interface


Kotlin MutableSet interface is a generic unordered collection of elements. MutableSet interface does not support duplicate elements. This interface is mutable so its methods support read-write functionality supports adding and removing elements.
Set interface uses mutableSetOf() function to create the list of object of set interface which contains list of elements.


MutableSet Interface declaration


interface MutableSet : Set, MutableCollection (source)  

Inherited Properties of MutableSet Interface


Properties Description
abstract val size: Int It returns the size of collection.

Functions of MutableSet Interface


Kotlin MutableSet interface has several functions. Some of its functions are mention below.


Functions Description
abstract fun add(element: E): Boolean It adds the given element to the collection.
abstract fun addAll(elements: Collection< E>): Boolean It adds all the elements given collection to the current collection.
abstract fun clear() It removes all the elements from this collection.
abstract fun iterator(): MutableIterator< E> It returns an iterator over the elements of this object.
abstract fun remove(element: E): Boolean It removes a single specified element from this collection, if it is present in collection.
abstract fun removeAll(elements: Collection): Boolean It removes all the elements from current collection which are given in collection.
abstract fun retainAll(elements: Collection< E>): Boolean It retains only those elements in current collection which are present in specified collection.
abstract fun contains(element: E): Boolean It checks the specified element is contained in current collection.
abstract fun containsAll(elements: Collection< E>): Boolean It checks all the elements of specified collection are present in current collection.
abstract fun isEmpty(): Boolean If collection is empty (not containing any element) it returns true, otherwise it returns false.
fun < T> Iterable< T>.any(): Boolean It returns true if collection contains at least one element.
fun < T> Iterable< T>.any(predicate: (T) -> Boolean): Boolean It returns true if at least element matches the given the given predicate.
fun < T> Iterable< T>.distinct(): List< T> It returns a list which contains only distinct elements from the given collection.
fun < T> Iterable< T>.drop(n: Int): List< T> It returns a list which contains all elements except first n elements.
fun < T> Iterable< T>.elementAt(index: Int): T It returns an element at given index or throw an IndexOutOfBoundException if given index is not present in collection.
fun < T> Iterable< T>.elementAtOrElse( index: Int, defaultValue: (Int) -> T ): T It returns an element at given index or result of calling the defaultValue function if the index is out bounds in current collection.
fun < T : Comparable< T>> Iterable< T>.max(): T? It returns the largest element or null if there is no element in the collection.
fun < T : Comparable< T>> Iterable< T>.min(): T? It returns the smallest element or null if there is no element in the collection.
fun < T> MutableCollection< out T>.remove(element: T): Boolean It removes the single specified element if it in present in current collection.
fun < T> MutableCollection< out T>.removeAll( elements: Collection< T> ): Boolean It removes all the elements of current collection which are contained in specified collection.
fun < T> MutableCollection< out T>.retainAll( elements: Collection< T> ): Boolean It retains all the elements in current collection which are contained in specified collection.
fun < T> Iterable< T>.reversed(): List< T> It returns the elements in reversed order.

Kotlin MutableSet Interface Example 1


Let's create an example of MutableSet declaring and traversing its elements.


fun main(args: Array) {  
    val intmutableSet = mutableSetOf(2, 6, 4, 29, 4, 5)  
    val anymutableSet: Set = setOf(2, 6, 4, 29, 4, 5, "Ajay", "Ashu", "Ajay")  
    println("....intmutableSet....")  
    for(element in intmutableSet){  
        println(element)  
    }  
    println("....anymutableSet......")  
    for(element in anymutableSet){  
        println(element)  
    }  
}  


Output:
....intmutableSet....
2
6
4
29
5
....anymutableSet......
2
6
4
29
5
Ajay
Ashu


In the above example, elements "4" and "Ajay" are declared twice. But while traversing these MutableSet they are printed only once, this is because MutableSet interface does not support duplicate elements.


Kotlin MutableSet Interface Example 2 - add() and addAll()


fun main(args: Array) {  
    val intmutableSet = mutableSetOf< Int>(2, 6, 4, 29, 4, 5)  
    val mutableSet: MutableSet< Int> = mutableSetOf< >(6,8,11,22)  
  
    println("....intmutableSet....")  
    for(element in intmutableSet){  
        println(element)  
    }  
    intmutableSet.add(10)  
    println("....intmutableSet.add(10)....")  
    for(element in intmutableSet){  
        println(element)  
    }  
  
    intmutableSet.addAll(mutableSet)  
    println("....intmutableSet.addAll(mutableSet)....")  
    for(element in intmutableSet){  
        println(element)  
    }  
}  


Output:
....intmutableSet....
2
6
4
29
5
....intmutableSet.add(10)....
2
6
4
29
5
10
....intmutableSet.addAll(mutableSet)....
2
6
4
29
5
10
8
11
22

Kotlin MutableSet Interface Example 3 - remove() and removeAll()


fun main(args: Array) {  
    val intmutableSet = mutableSetOf(2, 6, 4, 29, 4, 5)  
    val mutableSet: MutableSet = mutableSetOf(6,8,11,22)  
  
    println("....intmutableSet....")  
    for(element in intmutableSet){  
        println(element)  
    }  
    intmutableSet.remove(29)  
    println("....intmutableSet.remove(29)....")  
    for(element in intmutableSet){  
        println(element)  
    }  
    intmutableSet.removeAll(mutableSet)  
    println("....intmutableSet.removeAll(mutableSet)....")  
    for(element in intmutableSet){  
        println(element)  
    }  
}  


Output:
....intmutableSet....
2
6
4
29
5
....intmutableSet.remove(29)....
2
6
4
5
....intmutableSet.removeAll(mutableSet)....
2
4
5


Kotlin MutableSet Interface Example 4 - contains() and containsAll()
fun main(args: Array) {  
    val mutableSet1 = mutableSetOf(2, 6, 4, 29, 4, 5)  
    val mutableSet2: MutableSet = mutableSetOf(6,8,11,22)  
    val mutableSet3: MutableSet = mutableSetOf(2,4,6)  
  
    println("....mutableSet1....")  
    for(element in mutableSet1){  
        println(element)  
    }  
    println("....mutableSet2....")  
        println(mutableSet2)  
    println("....mutableSet3....")  
        println(mutableSet3)  
    println("....mutableSet1.contains(29)....")  
    println(mutableSet1.contains(29))  
  
    println("....mutableSet1.containsAll(mutableSet2))....")  
    println(mutableSet1.containsAll(mutableSet2))  
    println("....mutableSet1.containsAll(mutableSet3))....")  
    println(mutableSet1.containsAll(mutableSet3))  
}  


Output:
....mutableSet1....
2
6
4
29
5
....mutableSet2....
[6, 8, 11, 22]
....mutableSet3....
[2, 4, 6]
....mutableSet1.contains(29)....
true
....mutableSet1.containsAll(mutableSet2))....
false
....mutableSet1.containsAll(mutableSet3))....
true


Kotlin MutableSet Interface Example 5 - isEmpty() and any()
fun main(args: Array) {  
    val mutableSet1 = mutableSetOf(2, 6, 4, 29, 4, 5)  
  
    println("....mutableSet1....")  
    for(element in mutableSet1){  
        println(element)  
    }  
    println("....mutableSet1.isEmpty()....")  
    if(mutableSet1.isEmpty())  
        println("mutableSet1 is empty, not contain any element")  
    else  
        println("mutableSet1 is not empty, contains element")  
  
    println("....mutableSet1.any()....")  
    if(mutableSet1.any())  
        println("mutableSet1 contain at least one or more elements")  
    else  
        println("mutableSet1 not contain any element")  
}  


Output:
....mutableSet1....
2
6
4
29
5
....mutableSet1.isEmpty()....
mutableSet1 is not empty, contains element
....mutableSet1.any()....
mutableSet1 contain at least one or more elements

Kotlin MutableSet Interface Example 6 - first(), indexOf() and drop()


fun main(args: Array) {  
    val mutableSet1 = mutableSetOf(2, 6, 4, 29, 4, 5)  
  
    println("....mutableSet1....")  
    for(element in mutableSet1){  
        println(element)  
    }  
    println("....mutableSet1.first()....")  
    println(mutableSet1.first())  
  
    println("...mutableSet1.indexOf(4)...")  
    println(mutableSet1.indexOf(4))  
  
    println("...mutableSet1.drop(3)...")  
    println(mutableSet1.drop(3))  
}  


Output:
....mutableSet1....
2
6
4
29
5
....mutableSet1.first()....
2
...mutableSet1.indexOf(4)...
2
...mutableSet1.drop(3)...
[29, 5]




Hi I am Pluto.