https://kotlinlang.org logo
Title
a

Alejandro Rios

09/21/2021, 7:30 PM
Hello all, not sure if this is a noob question but here it goes: How can I get the difference between two List/ArrayList regardless of the order of the sentence? I mean, for example these two lists
val first = listOf(1, 2, 3, 4, 5)
 val second = listOf(1, 2, 3)
If I use
first.minus(second)
I’ll get
[4, 5]
but If I use
second.minus(first)
I’ll get
[]
p

Paul Griffith

09/21/2021, 7:40 PM
You can convert to sets and get the symmetric difference, though there's no convenient standard library function, interestingly
a

Alejandro Rios

09/21/2021, 7:50 PM
Thanks @Paul Griffith
j

Javier

09/21/2021, 10:39 PM
They have added recently the
intersect
function to the stdlib, I think they should add the opposite too
i

ilya.gorbunov

09/22/2021, 12:37 PM
@Javier, No
intersect
was not added recently, it's been there quite a long time
@Alejandro Rios There's a request about adding symmetric difference to stdlib, https://youtrack.jetbrains.com/issue/KT-11330, but we're not sure about its use cases. Can you describe yours?
j

Javier

09/22/2021, 1:28 PM
Maybe union was? Don't remember then. I am going to like that issue, I had the difference as extension function since 2019.
a

Alejandro Rios

09/23/2021, 1:01 AM
That's really cool, thanks for the update
i

ilya.gorbunov

09/23/2021, 4:41 PM
@Javier How do you use your extension function in your code? What tasks do you solve with it?
j

Javier

09/23/2021, 7:00 PM
For example in an android app, we had multiple lists of quizzes, generally a pair of lists, and was a requirement to get the difference of quizzes to show them to the user.