新版本的变更 / Kotlin 1.6

val flag = true

// *** before: ***
val oldList = mutableListOf<String>()
oldList.add("one")
if (flag) oldList.add("two")
val resultOld: List<String> = oldList   // the same mutable object underneath

// *** in version 1.6: ***
// buildList, buildMap and buildSet became Stable
val result = buildList {
    add("one")
    if (flag) add("two")
    addAll(listOf("three""four"))
}                                        // a read-only List<String>

val map = buildMap<StringInt> {
    put("a"1)
    putAll(mapOf("b" to 2))
    remove("a")
}

val set = buildSet<String> {
    addAll(result)
    add("one")                           // the duplicate is dropped
}

fun main() {
    println(result)
    println(map)
    println(set)
}