Kotlin Set : setOf()
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> setOf( vararg elements: T): Set<T>
Description:
- This function returns a new read only set of given elements.
- The elements are iterated over, according as they are stored .
Kotlin program of setOf() function :
Kotlin
fun main(args: Array<String>) { //declaring a set of strings val seta = setOf( "Beginner" , "for" , "Beginner" ) //declaring a set of characters val setb = setOf( "G" , "f" , "g" ) //declaring a set of integers val setc = setOf( 1 , 2 , 3 , 4 ) //traversing through a set of strings for (item in seta) print( item ) println() //traversing through a set of characters for (item in setb) print( item ) println() //traversing through a set of integers for (item in setc) print( "$item " ) } |
Output:
w3wiki Gfg 1 2 3 4
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 = setOf( "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 = setOf( 1 , 2 , 3 , 4 , "Kohli" , "Smith" , "Root" , "Malinga" , "Rohit" , "Dhawan" ) 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
Set Basics –
Here we will discuss basics functions like count(), max(), min(), sum(), average().
Kotlin program of using basic functions –
Kotlin
fun main(args: Array<String>) { 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?
Kotlin program of using contains() and containsAll() function –
Kotlin
fun main(args: Array<String>){ val captains = setOf( 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> setOf(): Set<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 = setOf<String>() //creating an empty set of integers val setb =setOf<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 []
Sure, here’s an example code that demonstrates how to use the setOf() function in Kotlin
Kotlin
fun main() { val fruits = setOf( "apple" , "banana" , "cherry" ) val hasApple = fruits.contains( "apple" ) val hasOrange = fruits.contains( "orange" ) println( "Fruits: $fruits" ) println( "Does the set contain an apple? $hasApple" ) println( "Does the set contain an orange? $hasOrange" ) 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().