Kotlin MutableSet

Introduction

In Kotlin, MutableSet is an interface that extends the Set interface and provides additional functions to support mutable operations. It is part of the kotlin.collections package and allows for modifications to the set, such as adding, removing, and updating elements.

Table of Contents

  1. What is MutableSet?
  2. Creating a MutableSet
  3. Common Operations
  4. Examples of MutableSet
  5. Real-World Use Case
  6. Conclusion

1. What is MutableSet?

The MutableSet interface in Kotlin is a generic interface that represents a mutable collection of unique elements. It extends the Set interface and adds functions for modifying the set.

Syntax

interface MutableSet<E> : Set<E>, MutableCollection<E>

2. Creating a MutableSet

You can create a MutableSet using the mutableSetOf function or by converting an existing set.

Example

val emptySet = mutableSetOf<Int>() // Creates an empty MutableSet
val setWithElements = mutableSetOf(1, 2, 3, 4) // Creates a MutableSet with elements
val fromSet = setOf(5, 6, 7).toMutableSet() // Converts a Set to MutableSet

3. Common Operations

MutableSet supports various operations for adding, removing, and accessing elements.

Adding Elements

  • add(element: E): Adds an element to the set. If the element already exists, it does nothing and returns false.
  • addAll(elements: Collection<E>): Adds all elements from the specified collection to the set.

Removing Elements

  • remove(element: E): Removes the specified element from the set.
  • removeAll(elements: Collection<E>): Removes all elements in the specified collection from the set.
  • retainAll(elements: Collection<E>): Retains only the elements in the set that are contained in the specified collection.
  • clear(): Removes all elements from the set.

Accessing Elements

  • iterator(): Returns an iterator over the elements in the set.
  • contains(element: E): Checks if the set contains the specified element.
  • containsAll(elements: Collection<E>): Checks if the set contains all elements in the specified collection.

Checking Size and Emptiness

  • size: Returns the number of elements in the set.
  • isEmpty(): Checks if the set is empty.
  • isNotEmpty(): Checks if the set is not empty.

4. Examples of MutableSet

Example 1: Adding Elements

add(element: E)

This example demonstrates how to add an element to the set.

fun main() {
    val set = mutableSetOf(1, 2, 3)
    set.add(4)
    println("Set after adding 4: $set") // Output: Set after adding 4: [1, 2, 3, 4]
}

Output:

Set after adding 4: [1, 2, 3, 4]

addAll(elements: Collection<E>)

This example demonstrates how to add all elements from a collection to the set.

fun main() {
    val set = mutableSetOf(1, 2, 3)
    val anotherSet = setOf(4, 5, 6)
    set.addAll(anotherSet)
    println("Set after adding all elements: $set") // Output: Set after adding all elements: [1, 2, 3, 4, 5, 6]
}

Output:

Set after adding all elements: [1, 2, 3, 4, 5, 6]

Example 2: Removing Elements

remove(element: E)

This example demonstrates how to remove a specified element from the set.

fun main() {
    val set = mutableSetOf(1, 2, 3)
    set.remove(2)
    println("Set after removing 2: $set") // Output: Set after removing 2: [1, 3]
}

Output:

Set after removing 2: [1, 3]

removeAll(elements: Collection<E>)

This example demonstrates how to remove all elements in a collection from the set.

fun main() {
    val set = mutableSetOf(1, 2, 3, 4, 5)
    val anotherSet = setOf(2, 4)
    set.removeAll(anotherSet)
    println("Set after removing elements: $set") // Output: Set after removing elements: [1, 3, 5]
}

Output:

Set after removing elements: [1, 3, 5]

retainAll(elements: Collection<E>)

This example demonstrates how to retain only the elements in the set that are contained in the specified collection.

fun main() {
    val set = mutableSetOf(1, 2, 3, 4, 5)
    val anotherSet = setOf(2, 4)
    set.retainAll(anotherSet)
    println("Set after retaining elements: $set") // Output: Set after retaining elements: [2, 4]
}

Output:

Set after retaining elements: [2, 4]

Example 3: Clearing the Set

clear()

This example demonstrates how to remove all elements from the set.

fun main() {
    val set = mutableSetOf(1, 2, 3)
    set.clear()
    println("Set after clearing: $set") // Output: Set after clearing: []
}

Output:

Set after clearing: []

Example 4: Checking Elements

contains(element: E)

This example demonstrates how to check if the set contains a specified element.

fun main() {
    val set = mutableSetOf("apple", "banana", "cherry")
    println("Set contains 'banana': ${set.contains("banana")}") // Output: Set contains 'banana': true
    println("Set contains 'grape': ${set.contains("grape")}") // Output: Set contains 'grape': false
}

Output:

Set contains 'banana': true
Set contains 'grape': false

containsAll(elements: Collection<E>)

This example demonstrates how to check if the set contains all elements in a specified collection.

fun main() {
    val set = mutableSetOf("apple", "banana", "cherry")
    val anotherSet = setOf("banana", "cherry")
    println("Set contains all elements: ${set.containsAll(anotherSet)}") // Output: Set contains all elements: true
}

Output:

Set contains all elements: true

Example 5: Checking Size and Emptiness

size, isEmpty(), and isNotEmpty()

This example demonstrates how to check the size of the set and if it is empty.

fun main() {
    val set = mutableSetOf(1, 2, 3)
    println("Size of set: ${set.size}") // Output: Size of set: 3
    println("Is set empty: ${set.isEmpty()}") // Output: Is set empty: false
    println("Is set not empty: ${set.isNotEmpty()}") // Output: Is set not empty: true
}

Output:

Size of set: 3
Is set empty: false
Is set not empty: true

Example 6: Iterating Over a Set

iterator()

This example demonstrates how to iterate over the elements in the set.

fun main() {
    val set = mutableSetOf('a', 'b', 'c', 'd')
    val iterator = set.iterator()
    while (iterator.hasNext()) {
        println(iterator.next())
    }
}

Output:

a
b
c
d

Comments