Kotlin – 29 – Mutable Sets in Kotlin

In Kotlin, a mutable set is a collection that allows you to add, remove, and modify its elements. Mutable sets are used when you need to work with a collection of distinct values that can change over time. In this article, we will explore mutable sets in Kotlin, how to create them, manipulate their elements, and perform common set operations.

Creating Mutable Sets

You can create mutable sets in Kotlin using the mutableSetOf() function. This function initializes an empty mutable set or populates it with initial elements. Here’s an example of creating a mutable set of strings:


val mutableFruits: MutableSet<String> = mutableSetOf("apple", "banana", "cherry")

In this example, mutableFruits is a mutable set containing three unique strings. You can add, remove, or modify elements in this set as needed.

Adding Elements

To add elements to a mutable set, you can use the add() method. This method appends an element to the set if it’s not already present. Here’s an example:


mutableFruits.add("date") // Adding an element

After executing this code, the mutableFruits set will contain four elements, including “date” if it wasn’t already in the set.

Removing Elements

To remove elements from a mutable set, you can use the remove() method. This method removes the specified element if it’s present in the set. Here’s an example:


mutableFruits.remove("banana") // Removing an element

After executing this command, the “banana” element will be removed from the set, leaving only the remaining elements.

Updating Elements

Mutable sets allow you to update elements by adding and removing them. To update an element’s value, you can remove the old element and add the updated one. Here’s an example:


mutableFruits.remove("cherry") // Removing the old element
mutableFruits.add("blueberry") // Adding the updated element

In this code, “cherry” is removed from the set, and “blueberry” is added in its place.

Iterating Over Mutable Sets

Iterating over the elements of a mutable set in Kotlin can be done using a for loop or the forEach() function, just like with read-only sets. Here’s a for loop example:


for (fruit in mutableFruits) {
    println(fruit)
}

You can also use the forEach() function to perform actions on each element:


mutableFruits.forEach { fruit -> println("Found $fruit") }
Common Set Operations

Mutable sets in Kotlin support various common operations, including:

  • size: Returns the number of elements in the set.
  • isEmpty(): Checks if the set is empty.
  • contains(): Checks if the set contains a specific element.
  • clear(): Removes all elements from the set.

Here’s an example demonstrating some of these operations:


val setSize = mutableFruits.size
val isEmpty = mutableFruits.isEmpty()
val hasApple = mutableFruits.contains("apple")
mutableFruits.clear() // Removes all elements from the set

These operations allow you to manipulate and query mutable sets effectively.

Conclusion

Mutable sets in Kotlin provide a versatile way to manage and manipulate collections of distinct elements that can change over time. They enable you to add, remove, and update elements as well as perform common set operations. Understanding how to work with mutable sets is essential for building dynamic and flexible Kotlin applications.