Converting a Kotlin Set to a List

Set.toList()

You can convert a Set to a List by using the toList() function.

fun main() {
    val set = setOf(1, 2, 3, 4, 5)

    val list = set.toList()

    println(list)
}

Output:

[1, 2, 3, 4, 5]

MutableSet.toMutableList()

The toMutableList() function returns a List that can add elements. In the example below, the add() function is used to add an element to list.

fun main() {
    val set = mutableSetOf(1, 2, 3, 4, 5)

    val list = set.toMutableList()
    list.add(6)

    println(list)
}

Output:

[1, 2, 3, 4, 5, 6]

ArrayList()

You can also convert to a List by passing set to the ArrayList() constructor function.

fun main() {
    val set = setOf(1, 2, 3, 4, 5)

    val list = ArrayList(set)

    println(set)
}

Output:

[1, 2, 3, 4, 5]

List.addAll()

You can also add the elements of set to a list declared as MutableList by using the addAll() function.

fun main() {
    val set = setOf(1, 2, 3, 4, 5)

    val list: MutableList<Int> = ArrayList()
    list.addAll(set)

    println(list)
}

Output:

[1, 2, 3, 4, 5]

References