본문 바로가기

Kotlin

#10.4 Kotlin SET and HASH SET. Mutable and Immutable Collections examples PART-4

fun main(args: Array<String>) {
    // "Set" contains unique elements
    // "HashSet" also contains unique elements but Sequence is not guaranteed in output

    var mySet = setOf<Int>( 1, 2, 3, 4, 4, 4) // Immutable, Read Only

    for (element in mySet){  // Using Individual Element (Objects)
        println(element)
    }
}

setOf 에서는 중복된 값이 있으면 하나만 출력한다. 

fun main(args: Array<String>) {
    // "Set" contains unique elements
    // "HashSet" also contains unique elements but Sequence is not guaranteed in output

    var mySet = mutableSetOf<Int>( 1, 2, 3, 4, 4, 4) // Immutable, Read Only
    mySet.remove(2)
    mySet.add(5)
    for (element in mySet){  // Using Individual Element (Objects)
        println(element)
    }
}

mutableSetof() : 값을 넣거나 뺄 수 있다.

fun main(args: Array<String>) {
    // "Set" contains unique elements
    // "HashSet" also contains unique elements but Sequence is not guaranteed in output

//    var mySet = mutableSetOf<Int>( 1, 2, 3, 4, 4, 4) // Immutable, Read Only
    var mySet = hashSetOf<Int>( 1, 9, 2, 3, 4, 4, 0, 4, 7, 8) // Immutable, Read Only
//    mySet.remove(2)
//    mySet.add(5)
    for (element in mySet){  // Using Individual Element (Objects)
        println(element)
    }
}

 

youtu.be/MJkCDXhLtDI