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

Inherited Properties of MutableSet Interface

PropertiesDescription
abstract val size: IntIt returns the size of collection.

Functions of MutableSet Interface

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

FunctionsDescription
abstract fun add(element: E): BooleanIt adds the given element to the collection.
abstract fun addAll(elements: Collection<E>): BooleanIt 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): BooleanIt removes a single specified element from this collection, if it is present in collection.
abstract fun removeAll(elements: Collection<E>): BooleanIt removes all the elements from current collection which are given in collection.
abstract fun retainAll(elements: Collection<E>): BooleanIt retains only those elements in current collection which are present in specified collection.
abstract fun contains(element: E): BooleanIt checks the specified element is contained in current collection.
abstract fun containsAll(elements: Collection<E>): BooleanIt checks all the elements of specified collection are present in current collection.
abstract fun isEmpty(): BooleanIf collection is empty (not containing any element) it returns true, otherwise it returns false.
fun <T> Iterable<T>.any(): BooleanIt returns true if collection contains at least one element.
fun <T> Iterable<T>.any(predicate: (T) -> Boolean): BooleanIt 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): TIt 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): BooleanIt 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.

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()

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()

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()

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()

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()

Output:

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