W3cubDocs

/Kotlin

mutableSetOf

Platform and version requirements: JVM (1.1), JS (1.1), Native (1.1)
fun <T> mutableSetOf(): MutableSet<T>

Returns an empty new MutableSet.

The returned set preserves the element iteration order.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val set = mutableSetOf<Int>()
println("set.isEmpty() is ${set.isEmpty()}") // true

set.add(1)
set.add(2)
set.add(1)

println(set) // [1, 2]
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T> mutableSetOf(vararg elements: T): MutableSet<T>

Returns a new MutableSet with the given elements. Elements of the set are iterated in the order they were specified.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val set = mutableSetOf(1, 2, 3)
println(set) // [1, 2, 3]

set.remove(3)
set += listOf(4, 5)
println(set) // [1, 2, 4, 5]
//sampleEnd
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-set-of.html