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 porogram of setOf() function :

filter_none

edit
close

play_arrow

link
brightness_4
code

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 ")
}

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

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"))
}

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

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")))
}

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

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 equl or not
    println("seta == setb is ${seta == setb}")
  
    println(seta) //printing first set
}

chevron_right



Output :

seta.isEmpty() is true
seta == setb is true
[]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.