kotlin
코틀린(Kotlin) Collections : Set Specific Operations
iosroid
2020. 5. 7. 18:30
코틀린의 collection 의 Set Specific Operations 에 대해 살펴보자.
원문 https://kotlinlang.org/docs/reference/set-operations.html 을 보며 정리.
두개의 collection 을 합칠때는 union() 함수를 사용한다. infix a union b 로도 사용한다.
intersect() 는 두 collection 에 모두 있는 element 를 찾는다. infix a intersect b
subtract() 는 한쪽 collection 에만 있는 element 를 찾는다. infix a subtract b
val numbers = setOf("one", "two", "three")
println(numbers union setOf("four", "five")) // [one, two, three, four, five]
println(setOf("four", "five") union numbers) // [four, five, one, two, three]
println(numbers intersect setOf("two", "one")) // [one, two]
println(numbers subtract setOf("three", "four")) // [one, two]
println(numbers subtract setOf("four", "three")) // [one, two]
set operation 은 List 도 지원하지만 set operation 의 결과는 여전히 Set 이기 때문에 중복되는 element 는 삭제된다.