Hanbit the Developer

Kotlin Documentation | Set-specific operations 본문

Mobile/Kotlin

Kotlin Documentation | Set-specific operations

hanbikan 2023. 5. 24. 15:48

Kotlin Documentation 시리즈에 대해

Category: Standard library - Collections

문서 링크: https://kotlinlang.org/docs/set-operations.html


  • union(), intersect(), subtract(): infix fun
val numbers = setOf("one", "two", "three")

println(numbers union setOf("four", "five"))
println(setOf("four", "five") union numbers)

println(numbers intersect setOf("two", "one"))
println(numbers subtract setOf("three", "four"))
println(numbers subtract setOf("four", "three")) // same output
println(numbers subtract setOf("four", "three")) // same output
[one, two, three, four, five]
[four, five, one, two, three]
[one, two]
[one, two]
[one, two]

위 함수들을 list에도 동일하게 사용할 수 있으나 Set을 반환한다.