Kotlin – Set interface

Kotlin Set interface is a generic unordered collection of elements. Set interface does not support duplicate elements. This interface is immutable in nature its methods supports read-only functionality of the set.Set interface uses setOf() function to create the list of object of set interface which contains list of elements.Set Interface declaration:

interface Set<out E>: Collection<E> (source)

Properties of Set Interface

PropertiesDescriptions
abstract val size: Int It returns the size of collection.

Functions of Set Interface

FunctionsDescriptions
abstract fun contains(element: E): Boolean It checks the mention element is present in this collection. If it contains element, it returns true else returns false.
abstract fun containsAll(elements: Collection<E>): Boolean It checks all the mention elements of specified collection are present in this collection. If it contains element, it returns true else returns false.
abstract fun isEmpty(): Boolean It returns true if the collection is empty (contains no elements) otherwise it returns false.
abstract fun iterator(): Iterator<E> It returns an iterator over the elements of set object.
fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean It returns true if all the elements matches with given predicate.
fun <T> Iterable<T>.any(): Boolean It returns true if the collection contains at least one element.
fun <T> Iterable<T>.count(predicate: (T) -> Boolean): Int It returns the total number of elements matching with given predicate.
fun <T> Iterable<T>.distinct(): List<T> It returns a list which contains only distinct elements from the given collection.
fun <T> Iterable<T>.drop(n: Int): List<T> It returns a list which contains all elements except first n elements.
fun <T> Iterable<T>.elementAtOrElse(
index: Int,
defaultValue: (Int) -> T
): T
It returns an element at given index or result of calling the defaultValue function if the index is out bounds in current collection.
  • Example 1 : Let create an example of declaring and traversing set element using setOf() function. In this example we create a set of Int type non generic and another generic set of Any type
fun main(args: Array<String>){
    val intSet = setOf(2,6,4,29,4,5)
    val mySet: Set<Any> = setOf(2,6,4,29,4,5,"Code","Rocky")
    println(".......print Int set.........")
    for(element in intSet){
        println(element)
    }
    println(".......print Any set.........")
    for(element in mySet){
        println(element)
    }

}

Output :

.......print Int set.........
2
6
4
29
5
.......print Any set.........
2
6
4
29
5
Code
Rocky

In the above example we declare element 4 twice in both intSet and mySet but while traversing them they print the element 4 only once. This is because the set interface does not support duplicate elements.

  • Example 2 – contains() and containsAll() : The contains() function checks the given element is present in current set or not. If it is contains in the set, the set returns true else returns false. Whereas containsAll() function checks all the elements of collection type are present in the current set or not. If the set contains all elements of collection type it returns true else false.
fun main(args: Array<String>){
    val mySet: Set<Any> = setOf(2,6,4,29,5,"Ashu","Ajay")
    val intSet = setOf(6,4,29)
    println(".......print Any set.........")
    for(element in mySet){
        println(element)
    }
    println("...mySet.contains\"Ashu\"...")
    println(mySet.contains("Ashu"))
    println("...mySet.contains(20)...")
    println(mySet.contains(20))
    println("...mySet.containsAll(intSet)...")
    println(mySet.containsAll(intSet))
}

Output :

.......print Any set.........
2
6
4
29
5
Ashu
Ajay
...mySet.contains"Ashu"...
true
...mySet.contains(20)...
false
...mySet.containsAll(intSet)...
true
  • Example 3 – isEmpty() and isNotEmpty() : The isEmpty() function checks the current set is empty. If the set is empty the isEmpty() function returns true otherwise it returns false. And isNotEmpty() checks the current set is not empty. If the set is not empty the isNotEmpty() function returns true else return false.
fun main(args: Array<String>){
    val mySet: Set<Any> = setOf(2,6,4,29,5,"Ashu","Ajay")
    println(".......print Any set.........")
    for(element in mySet){
        println(element)
    }
    println("...mySet.isEmpty()...")
    println(mySet.isEmpty())

    println("...mySet.isNotEmpty()...")
    println(mySet.isNotEmpty())
}

Output :

.......print Any set.........
2
6
4
29
5
Ashu
Ajay
...mySet.isEmpty()...
false
...mySet.isNotEmpty()...
true
  • Example 4 – drop() : The drop() function returns all the element except the first n elements of collection.
fun main(args: Array<String>){
    val mySet: Set<Any> = setOf(2,6,4,29,4,5,"Ajay","Ashu","Ajay")
    println(".......print Any set.........")
    for(element in mySet){
        println(element)
    }
    val remainList= mySet.drop(4)
    println(".......print Set after mySet.drop(4).........")
    for(element in remainList){
        println(element)
    }
}

Output :

.......print Any set.........
2
6
4
29
5
Ajay
Ashu
.......print Set after mySet.drop(4).........
5
Ajay
Ashu
  • Example 5 – elementAt() and elementAtOrNull() : The elementAt() function return element at given index and elementAtOrNull() function also return the element at given index , but if specified index does not contain element it returns null.
fun main(args: Array<String>){
    val mySet: Set<Any> = setOf(2,6,4,29,4,5,"Ajay","Ashu","Ajay")
    println(".......print Any set.........")
    for(element in mySet){
        println(element)
    }

    println(".......print mySet.elementAt(3).........")
    println(mySet.elementAt(3))

    println(".......print mySet.elementAtOrNull(5).........")
    println(mySet.elementAtOrNull(5))
}

Output :

.......print Any set.........
2
6
4
29
5
Ajay
Ashu
.......print mySet.elementAt(3).........
29
.......print mySet.elementAtOrNull(5).........
Ajay

I hope you found what you were looking for from this tutorial. If you want more Kotlin tutorials like this, then do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂

Leave a Comment

Your email address will not be published.