Set 相关操作
Kotlin 集合包中包含 set 常用操作的扩展函数: 找出集合间的交集、并集或差集。
要将两个集合合并为一个(并集),可使用 union()
函数。也能以中缀形式使用 a union b
。
注意,对于有序集合,操作数的顺序很重要。在结果集合中,
左侧操作数在前:
fun main() {
//sampleStart
val numbers = setOf("one", "two", "three")
// output according to the order
println(numbers union setOf("four", "five"))
// [one, two, three, four, five]
println(setOf("four", "five") union numbers)
// [four, five, one, two, three]
//sampleEnd
}
To find an intersection between two collections (elements present in both of them), use the intersect()
function.
To find collection elements not present in another collection, use the subtract()
function.
Both these functions can be called in the infix form as well, for example, a intersect b
:
fun main() {
//sampleStart
val numbers = setOf("one", "two", "three")
// same output
println(numbers intersect setOf("two", "one"))
// [one, two]
println(numbers subtract setOf("three", "four"))
// [one, two]
println(numbers subtract setOf("four", "three"))
// [one, two]
//sampleEnd
}
To find the elements present in either one of the two collections but not in their intersection, you can also use the union()
function.
For this operation (known as symmetric difference), calculate the differences between the two collections and merge the
results:
fun main() {
//sampleStart
val numbers = setOf("one", "two", "three")
val numbers2 = setOf("three", "four")
// merge differences
println((numbers - numbers2) union (numbers2 - numbers))
// [one, two, four]
//sampleEnd
}
You can also apply union()
, intersect()
, and subtract()
functions to lists.
However, their result is always a Set
. In this result, all the duplicate elements are merged into one and the index access is not available:
fun main() {
//sampleStart
val list1 = listOf(1, 1, 2, 3, 5, 8, -1)
val list2 = listOf(1, 1, 2, 2, 3, 5)
// result of intersecting two lists is a Set
println(list1 intersect list2)
// [1, 2, 3, 5]
// equal elements are merged into one
println(list1 union list2)
// [1, 2, 3, 5, 8, -1]
//sampleEnd
}