https://kotlinlang.org logo
#android
Title
# android
l

Lilly

04/06/2021, 12:33 AM
It creates a new list every time right? I forgot about
when
:
Copy code
destination.filterKeys { key ->
    when (key) {
      in list1, list2, list3 -> true
      else -> false
    }
}
I'm fine with this, if no one else has a more convenient solution
e

ephemient

04/06/2021, 1:16 AM
you mean
in list1, in list2, in list3 ->
right?
it would be equivalent to
key in list1 || key == list2 || key == list3
as you wrote it
if you had a lot of lists, then maybe
Copy code
listOf(list1, list2, list3).any { key in it }
would make sense. maybe not for just 3 of them, though
l

Lilly

04/06/2021, 1:28 AM
it would be equivalent to 
key in list1 || key == list2 || key == list3
 as you wrote it
if this is true, I made a mistake. Thanks for clarification @ephemient
v

Vampire

04/06/2021, 7:49 AM
Yep, same error as Brians solution
listOf(list1, list2, list3).all { key in it }
Optimally with the
listOf
outside the lambda
e

ephemient

04/06/2021, 7:55 AM
ah going back to previous conversation started in another thread, I see. yeah the
when
isn't going to do the right thing regardless
depends on various factors, but creating an intersection set up front may be a good idea
common = list1.toSet() intersect list2 intersect list3; key in common
for example
if you have more, it would be better to accumulate like common = list1.toMutableSet() common.retainAll(list2) common.retainAll(list3)
l

Lilly

04/07/2021, 1:54 AM
Thanks guys!
3 Views