How to declare and initialize a MutableSet in Kotlin?
I would use mutableSetOf:
val s = mutableSetOf(1, 2, 3)
which is a kotlin.collections.LinkedHashSet under the hood.
Kotlin doesn't have its own implementations of collection interfaces. You can use standard Java sets such as HashSet
or TreeSet
, or any other set implementation out there. HashSet is the most popular one, and the preferred way of creating a HashSet from given elements is using the hashSetOf
function:
val set: MutableSet<Int> = hashSetOf(1, 2, 3)