Kotlin mutableSetOf() Example

This post shows usage of mutableSetOf() method in Kotlin with an example. With mutableSetOf(), we can create mutable sets in Kotlin.

Kotlin mutableSetOf() Example

The following example creates a mutable set and presents several of its methods.
package net.sourcecodeexamples.kotlin

fun main(args: Array < String > ) {

    val nums = mutableSetOf(3, 4, 5)

    nums.add(6)
    nums.add(7)
    nums.addAll(setOf(8, 9, 10))

    println(nums)

    nums.remove(10)

    println(nums)

    nums.retainAll(setOf(12, 14, 16, 18))

    println(nums)

    nums.clear()

    if (nums.isEmpty()) println("The set is empty")
    else println("The set is not epty")
}
Output:
[3, 4, 5, 6, 7, 8, 9, 10]
[3, 4, 5, 6, 7, 8, 9]
[]
The set is empty





Comments