In Kotlin, you can create a list of sets using the `mutableListOf` function for the outer list and the `mutableSetOf` function for the inner sets.
In this example,
set1
and set2
using the mutableSetOf
function.listOfSets
using the mutableListOf
function and initialize it with the inner sets.fun main() {
val set1 = mutableSetOf(1, 2, 3)
val set2 = mutableSetOf('a', 'b', 'c')
val listOfSets = mutableListOf(set1, set2)
listOfSets.forEach { set ->
println(set)
}
}
[1, 2, 3] [a, b, c]
In this example,
listOfSetsEmpty
using the mutableListOf
function.emptySet1
and emptySet2
.emptySet1
and emptySet2
to the outer list listOfSetsEmpty
.fun main() {
val emptySet1 = mutableSetOf<Int>()
val emptySet2 = mutableSetOf<Char>()
val listOfSetsEmpty = mutableListOf(emptySet1, emptySet2)
listOfSetsEmpty.forEach { set ->
println(set)
}
}
[] []
In this tutorial, we learned How to create a List of Sets in Kotlin language with well detailed examples.