Kotlin Set and MutableSet Collections Tutorial

In this section, we will learn what the Set and MutableSet are and how to use them in Kotlin.

What is Set Collection in Kotlin?

The Set in Kotlin represents a collection type object like arrays and lists where we can store values (objects) in there.

Its main difference compared to arrays and lists is that every element in a Set object must be unique! Basically, you can’t have a duplicated element in a set object.

If you try to add a duplicated element to such object, it’ll be ignored.

Alright, now let’s move on and see how we can actually create a Set object.

How to create a Set object in Kotlin? Kotlin setOf() Function:

In order to create a Set object, we use the setOf() function.

Simply call this function and pass one or more values as its argument and they will be used as the elements of a Set object.

Note that a Set object is immutable! This means after creating a Set object, you can’t increase or decrease its elements.

Example: creating a Set object in Kotlin using setOf() Function:

fun main(){

    var colors = setOf("Red","Blue","Blue","Red","Black","Green")

    println(colors.toString())

}

Output:

[Red, Blue, Black, Green]

As you can see, we’ve added the value Red two times and same for the value Blue, but we can see that the duplicated values are ignored and basically dropped from the Set.

How to Access Elements of a Set Object?

Unlike Arrays and Lists, the elements in a Set object are not ordered! This means when you add an element to a Set object for the first time, it might not be the first element when calling the Set object to take out its elements. For this reason, there’s no index number associated with each element in a Set object.

So in order to access the elements of a Set object, we use the for loop and iterate through the entire elements.

Example: accessing the elements of a Set object

fun main(){
    var colors = setOf("Red","Blue","Blue","Red","Black","Green")

    for (element in colors){
        println("The element of the Set is: $element")
    }
}

Output:

The element of the Set is: Red

The element of the Set is: Blue

The element of the Set is: Black

The element of the Set is: Green

Kotlin Set contains() Function

The contains function is used to see if a Set object has a specific element in it or not.

The function takes one argument, and that is the value we want to search for it in the target Set object.

If the value was in the Set, the return value of this function will be true. Otherwise, the value false will return instead.

Example: using Set contains() function in Kotlin

fun main(){
    var colors = setOf("Red","Blue","Blue","Red","Black","Green")

    if (colors.contains("Black")){
        println("Yes, the Set has the value Black in it")
    }else{
        println("No, the set doen't have the value Black in it")
    }
}

Output:

Yes, the Set has the value Black in it

What is MutableSet Collection in Kotlin?

The MutableSet is the same as the Set collection, where we can store unique values (objects).

But the main difference between MutableSet and Set is that we can add or remove elements from a MutableSet object! Basically, a MutableSet object, as its name suggests, is mutable.

How to create a MutableSet object in Kotlin? Kotlin mutableSetOf() Function:

In order to create a MutableSet object, we call the mutableSetOf() function and pass any value as the argument of the function to become part of the elements of the new MutableSet object.

Example: creating a MutableSet object in Kotlin using mutableSetOf() function

fun main(){
    var mSet = mutableSetOf("Omid",10,"Jack",true,false, 100.4)

    println(mSet.toString())
}

Output:

[Omid, 10, Jack, true, false, 100.4]

Kotlin MutableSet Object Adding Element

As mentioned before, a MutableSet object is capable of taking new elements dynamically at runtime.

To do that, we use the add() function.

Simply put, the value you want to add to the Set as the argument of this function and if that value wasn’t added to the Set already, it’ll be added then.

Note: if the value was already in the Set, the add() function won’t add the argument to the Set.

Example: adding elements to a MutableSet object using add() function

fun main(){
    var mSet = mutableSetOf("Omid",10,"Jack",true,false, 100.4)

    println("The elements of the MutableSet before calling the add() function: ")
    println(mSet.toString())

    mSet.add("Elon")
    mSet.add("Monica")
    mSet.add("Jack")

    println("The elements of the MutableSet after calling the add() function: ")
    println(mSet.toString())
}

Output:

The elements of the MutableSet before calling the add() function:

[Omid, 10, Jack, true, false, 100.4]

The elements of the MutableSet after calling the add() function:

[Omid, 10, Jack, true, false, 100.4, Elon, Monica]

Kotlin MutableSet Object: Removing Elements using remove() Function

We can remove elements from a MutableSet object as well.

To do that, we call the remove() function and pass the value that we want to remove from the Set.

Note that if the value we set as the argument of this function wasn’t part of the elements of the Set, then nothing will happen.

Example: removing elements from a MutableSet object using the remove() function

fun main(){
    var mSet = mutableSetOf("Omid",10,"Jack",true,false, 100.4)

    println("The elements of the MutableSet before calling the remove() function: ")
    println(mSet.toString())

    mSet.remove("Elon")
    mSet.remove("Monica")
    mSet.remove("Jack")
    mSet.remove(10)

    println("The elements of the MutableSet after calling the remove() function: ")
    println(mSet.toString())
}

Output:

The elements of the MutableSet before calling the remove() function:

[Omid, 10, Jack, true, false, 100.4]

The elements of the MutableSet after calling the remove() function:

[Omid, true, false, 100.4]

Kotlin MutableSet Object: Clearing Elements using clear() Function

If for whatever reason you wanted to remove the entire elements of a MutableSet object, you can call the clear() function on top of the target Set and it will clear out the entire elements.

Example: using clear() function to clear the elements of a MutableSet object

fun main(){
    var mSet = mutableSetOf("Omid",10,"Jack",true,false, 100.4)

    println("The elements of the MutableSet before calling the clear() function: ")
    println(mSet.toString())

    mSet.clear() 
    
    println("The elements of the MutableSet after calling the clear() function: ")
    println(mSet.toString())
}

Output:

The elements of the MutableSet before calling the clear() function:

[Omid, 10, Jack, true, false, 100.4]

The elements of the MutableSet after calling the clear() function:

[]

Kotlin MutableSet: Getting the Length

If we want to get the size (the number of elements) of a MutableSet object, we can call the size property on either MutableSet or Set object.

Example: getting the length of a Set object using the size property

fun main(){
    var mSet = mutableSetOf("Omid",10,"Jack",true,false, 100.4)

    println("The size of the MutableSet before calling the clear() function: ")
    println(mSet.size)

    mSet.clear() 

    println("The size of the MutableSet after calling the clear() function: ")
    println(mSet.size)
}

Output:

The size of the MutableSet before calling the clear() function:

6

The size of the MutableSet after calling the clear() function:

0
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies