How to declare a MutableSet<Int>
with the values 1, 2 and 3?
To create an empty Set in Kotlin, call setOf() function with no values passed to it. setOf() function creates an immutable Set with the values provided, if any. To create an empty mutable set, call mutableSetOf() function with no values passed to it.
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)
I would use mutableSetOf:
val s = mutableSetOf(1, 2, 3)
which is a kotlin.collections.LinkedHashSet under the hood.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With