언어/Kotlin

코틀린 set

김어찐 2022. 5. 26. 12:21
728x90
package set

fun main() {
    val set1 = setOf(1,5,10,5,10)
    println("set1 = ${set1}")

    val set2 = mutableSetOf<Int>()
    println("set2 = ${set2}")

    for (item in set1) {
        println("item = ${item}")
    }
    println("set1.size = ${set1.size}")

    println("set2 = ${set2}")
    set2.add(10)
    set2.add(20)
    set2.add(30)
    set2.addAll(listOf(40,50,60))
    println("set2 = ${set2}")

    set2.remove(30)
    println("set2 = ${set2}")

    val toList = set2.toList()
    val toMutableList = set2.toMutableList()

    val toSet = toList.toSet()
    val toMutableSet = toList.toMutableSet()
}
728x90