Skip to content
Related Articles

Related Articles

Improve Article

Kotlin Set : setOf()

  • Last Updated : 15 Jul, 2021

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("Geeks" , "for", "geeks")
    //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: 

Geeksforgeeks
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
[]

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :