问题
Is there a Kotlin stdlib function to find the symmetric difference between two sets? So given two sets [1, 2, 3]
and [1, 3, 5]
the symmetric difference would be [2, 5]
.
I've written this extension function which works fine, but it feels like an operation that should already exist within the collections framework.
fun <T> Set<T>.symmetricDifference(other: Set<T>): Set<T> {
val mine = this subtract other
val theirs = other subtract this
return mine union theirs
}
EDIT: What is the best way get the symmetric difference between two sets in java? suggests Guava or ApacheCommons, but I'm wondering if Kotlin's stdlib supports this.
来源:https://stackoverflow.com/questions/65361494/find-the-symmetric-difference-between-two-sets-in-kotlin