AmrJyniat
01/09/2023, 9:38 AMA
class like this:
data class A(val listOfB: List<B>)
And B
is a sealed interface which has multiple children like this:
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?Javier
01/09/2023, 9:45 AMval cList = aList.flatMap { it.filterIsInstance<C>() }
AmrJyniat
01/09/2023, 9:54 AMJavier
01/09/2023, 10:02 AMaList.filter { a -> a.listOfB.all { b -> b is C } }
AmrJyniat
01/09/2023, 10:12 AMA
item if wasn't all B
items type is C
, but what I want to filter is each B
list not the A
list.Javier
01/09/2023, 10:19 AMJavier
01/09/2023, 10:20 AMval filteredAList: List<A> = aList.map { a -> a.copy(listOfB = a.listOfB.filterIsInstance<C>()) }
AmrJyniat
01/09/2023, 10:26 AM