In Kotlin, a Set is a generic unordered collection of elements that does not allow duplicate elements. Kotlin provides two main types of sets:
- Immutable Set: Created using setOf() – supports only read-only operations.
- Mutable Set: Created using mutableSetOf() – supports both read and write operations.
In this article, we are going to learn about immutable set, i.e., setOf().
To learn about Mutable set, refer to Kotlin mutableSetOf()
Syntax:
fun <T> setOf( vararg elements: T): Set<T>
Description:
The setOf() function returns a new read-only set containing the given elements. The elements are iterated in the order they are stored, but remember, Set does not guarantee a consistent element order (especially if you're using the standard Set interface and not LinkedHashSet).
Example of setOf() function :
fun main() {
val set = setOf("Geeksforgeeks", "Gfg", 1, 2, 3, 4)
for (element in set) {
println(element)
}
}
Output:
Geeksforgeeks
Gfg
1
2
3
4
Set Indexing -
Kotlin provides certain functions for indexing:
- indexOf(element): Returns the first index of the specified element.
- lastIndexOf(element): Returns the last index (relevant only for lists; not typically used with sets).
- elementAt(index): Returns the element at a specific index.
Example of using index -
fun main() {
val set = listOf("Branch", "Leaf", "Root", "Stem", "Root")
println("The element at index 2 is: ${set.elementAt(2)}")
println("The index of element is: ${set.indexOf("Leaf")}")
println("The last index of element is: ${set.lastIndexOf("Root")}")
}
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.
Example -
fun main() {
val set = setOf(1, "Kohli", "Dhawan")
println("The first element of the set is: ${set.first()}")
println("The last element of the set is: ${set.last()}")
}
Output:
The first element of the set is: 1
The last element of the set is: Dhawan
Set Basics -
Here we will discuss basics functions like count(), max(), min(), sum(), average().
Example of using basic functions -
fun main() {
val num = setOf(1 ,2, 3, 4, 5, 6, 7, 8)
println("The number of element in the set is: "+num.count())
println("The maximum element in the set is: "+num.max())
println("The minimum element in the set is: "+num.min())
println("The sum of the elements in the set is: "+num.sum())
println("The average of elements in the set is: "+num.average())
}
Output:
The number of element in the set is: 8
The maximum element in the set is: 8
The minimum element in the set is: 1
The sum of the elements in the set is: 36
The average of elements in the set is: 4.5
contains() and containsAll() functions -
Both the methods are used to check whether an element is present in the set or not?
Example of using contains() and containsAll() function -
fun main() {
val set = setOf("Dhawan", "Kohli", "Rohit", 1, 2, 3)
println("The set contains the element Dhawan? ${set.contains("Dhawan")}")
println("The set contains the element 5? ${set.contains(5)}")
println("The set contains all elements 1, 2, 3? ${set.containsAll(listOf(1, 2, 3))}")
}
Output:
The set contains the element Dhawan? true
The set contains the element 5? false
The set contains all elements 1, 2, 3? true
Empty Set and isEmpty() Function -
val emptySet = setOf<String>()This syntax returns an empty set of specific type, for eg. here we have used , String.
Example of using isEmpty() function -
fun main() {
val seta = setOf<String>()
val setb = setOf<String>()
println("seta.isEmpty() is ${seta.isEmpty()}")
println("seta == setb is ${seta == setb}")
println(seta)
}
Output :
seta.isEmpty() is true
seta == setb is true
[]
Full Example Demonstrating setOf() -
fun main() {
val fruits = setOf("apple", "banana", "cherry")
println("Fruits: $fruits")
println("Does the set contain an apple? ${fruits.contains("apple")}")
println("Does the set contain an orange? ${fruits.contains("orange")}")
for (fruit in fruits) {
println(fruit)
}
}
Output:
Fruits: [apple, banana, cherry]
Does the set contain an apple? true
Does the set contain an orange? false
apple
banana
cherry
Advantages of setOf() function in Kotlin:
- It is a simple and convenient way to create an immutable set of elements.
- Since the set is immutable, its contents cannot be modified once it has been created, making it a safe option for use in multithreaded environments.
- The setOf() function ensures that the set does not contain any duplicate elements, which can help to prevent bugs in your code.
Disadvantages of setOf() function in Kotlin:
- Since the set is immutable, you cannot add or remove elements from it once it has been created. This may be a disadvantage if you need to modify the contents of the set during the runtime of your program.
- If you need to create a mutable set that can be modified at runtime, you will need to use a different function, such as mutableSetOf().