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.
fun <T> mutableSetOf( vararg elements: T): MutableSet<T>
- This function returns a set of given elements, which can be both read and written.
- The returned set preserves the element iteration order.
Kotlin porogram of mutableSetOf() function :
[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.
[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 –
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 –
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 .
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 –
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 –
seta.isEmpty() is true seta == setb is true 
- Kotlin | Math.abs() method with Examples
- RadioButton in Kotlin
- Chronometer in Kotlin
- Introduction to Kotlin
- HorizontalScrollView in Kotlin
- Kotlin functions
- TextView in Kotlin
- ProgressBar in Kotlin
- Kotlin Hashmap
- RatingBar in Kotlin
- ImageButton in Kotlin
- ImageSwitcher in Kotlin
- Switch in Kotlin
- Button in Kotlin
- CheckedTextView in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.