본문 바로가기
언어/Kotlin

코틀린 set

by 김어찐 2022. 5. 26.
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

'언어 > Kotlin' 카테고리의 다른 글

코틀린 map  (0) 2022.05.26
코틀린 리스트  (0) 2022.05.26
코틀린 문자열  (0) 2022.05.26
코틀린 배열  (0) 2022.05.26
코틀린 고차함수  (0) 2022.05.26