I have `A` class like this: ```data class A(val li...
# android
a
I have
A
class like this:
Copy code
data class A(val listOfB: List<B>)
And
B
is a sealed interface which has multiple children like this:
Copy code
sealed interface B
object C: B
object D: B
.....
Ok, now I have a list of
A
and each item holds a list of
B
, what I want is to return the
A
list contains only the
C
items in each
B
list. I mean that I want to remove each item that isn't
C
from each
B
list in
A
list What is the straight forward way to do that?
j
Copy code
val cList = aList.flatMap { it.filterIsInstance<C>() }
a
Sorry for the unclear question at first, I updated the question, is it clear now?
j
Copy code
aList.filter { a -> a.listOfB.all { b -> b is C } }
a
Doesn't work, This will not return the
A
item if wasn't all
B
items type is
C
, but what I want to filter is each
B
list not the
A
list.
j
Then you need to use copy or arrow optics probably
Copy code
val filteredAList: List<A> = aList.map { a -> a.copy(listOfB = a.listOfB.filterIsInstance<C>()) }
🙌 1
a
Yeah, that works, Thank you!