3 minutes read

In previous topics, you got familiar with Set and how it differs from MutableList. Now, we will take a look at MutableSet: when and how it is used and how exactly it differs from Set and MutableList.

Introduction

MutableSet is an unordered collection of elements that does not allow duplicates. Unlike Set which is an immutable collection, MutableSet is mutable or modifiable: you can freely add and remove elements.

Imagine you're making a list of groceries you need to buy:

val groceries = setOf("Banana", "Strawberry")
println(groceries) // [Banana, Strawberry]

But what if you remembered something later and need to add more elements? This is exactly the kind of situation where MutableSet is useful. MutableSet supports the addition of elements:

val groceries = mutableSetOf("Banana", "Strawberry")
groceries.add("Water")
println(groceries) // [Banana, Strawberry, Water]

You might ask: why not just use a MutableList? The MutableList unlike MutableSet allows you to have duplicates:

val groceries = mutableSetOf("Banana", "Banana", "Strawberry")
println(groceries) // [Banana, Strawberry]

val secondGroceries = mutableListOf("Banana", "Banana", "Strawberry")
println(secondGroceries) // [Banana, Banana, Strawberry]

Creating a MutableSet

You can initialize MutableSet the following way:

val students = mutableSetOf("Joe", "Elena", "Bob")
println(students) // [Joe, Elena, Bob]

Here, we didn't even need to specify the type for our objects because it can be derived from the context. However, note that if you create an empty MutableSet, you do have to specify the type:

val points = mutableSetOf<Int>()
println(points) // []

You can also transform Set into a MutableSet with the help of the function toMutableSet():

val students = setOf("Joe", "Elena", "Bob").toMutableSet()
students.add("Bob")
println(students) // [Joe, Elena, Bob]

Adding elements

MutableSet has the same properties and methods as Set: size, isEmpty(), indexOf(element), contains(element), first(), last(), and so on.

Also, MutableSet offers additional functionality for changing the contents:

  • add(element) is a method that adds the specified element to the set;

  • addAll(elements) adds all the elements of the specified collection to the set.

Let's take a look at an example. Imagine that you're working in a pair with your colleague, and your task is writing down the recorded words quickly and correctly. You decide to transcribe together and then combine the results, and here is how it goes:

val words = mutableSetOf<String>("Apple", "Coke")
val friendsWords = mutableSetOf<String>("Banana", "Coke")

words.add("Phone")
words.add("Controller")

friendsWords.add("Phone")
friendsWords.add("Pasta")
friendsWords.add("Pizza")

words.addAll(friendsWords)

println(words) // [Apple, Coke, Phone, Controller, Banana, Pasta, Pizza]

Removing elements

You might also need to remove some or all of the elements from your set. Let's see how this can be done.

  • remove(element) removes the specified element;

  • clear() removes all the elements from the current collection;

  • removeAll(elements) removes all the elements that are also contained in the specified collection (see the example below).

val groceries = mutableSetOf("Apple", "Water", "Banana", "Pen")

groceries.remove("Apple")
println(groceries) // [Water, Banana, Pen]

val uselessGroceries = setOf("Banana", "Pen")
groceries.removeAll(uselessGroceries)
println(groceries) // [Water]

groceries.clear()
println(groceries) // []

Of course, there are many other useful MutableSet methods: check out kotlinlang.org and get familiar with them!

Iterating through elements

You can iterate through the elements of a MutableSet with the help of the for loop. Here is a simple example:

val places = mutableSetOf("Saint-Petersburg", "Moscow", "Grodno", "Rome")

for (place in places) {
    println(place)
}

// Saint-Petersburg
// Moscow
// Grodno
// Rome

Conclusion

Now you know the difference between a Set and a MutableSet. Set is an immutable collection, so it makes sense to use it when you don't want the contents to change. If you do, then MutableSet is a better choice, and now you know how to initialize it, add and remove elements, and iterate through them. Note that you can also solve problems in this topic with immutable sets if you're an avid programmer, but while we're just learning, use mutable sets.

188 learners liked this piece of theory. 2 didn't like it. What about you?
Report a typo