Hi. I have two data classes: ```data class ScopeGr...
# announcements
v
Hi. I have two data classes:
Copy code
data class ScopeGroup(
      val id: UUID,
      val scopeOptions: List<ScopeOption>
)
and
Copy code
data class ScopeOption(
      val id: UUID,
      val status: String
)
So, the service returns a list of
ScopeGroup
with several
ScopeOption
inside. I want to filter this list so that
ScopeGroup
has a list of only those
ScopeOption
that has a certain status. I did this:
Copy code
list.flatMap { group -> group.scopeOptions.filter { option -> option.status == "ACTIVE" } }
But it returns
List<ScopeOption>
. Is it possible to return
List<ScopeGroup>
instead?
m
list.filter{ group -> group.scopeOptions.all { option -> option.status == "ACTIVE" } } ?
v
This one compiles. Will run it now 🙂
m
reads as: I want to only get my scopegroups where ALL scopeotions are active
or do you not want that?
v
I want to only get my scopegroups where ALL scopeotions are active
I want exactly this.
m
👍
v
Looks like your example works, Thanks a lot, @Michael de Kaste!
j
@Vitali Plagov do you want a filtered
List<ScopeGroup>
, in which each ScopeGroup only has scopeOptions with status == "ACTIVE" ?
If so, the proposed solution does not give me that desired result.. am I missing something here ?
v
Hi @Jörg Winter, yes, my case is exactly as you wrote. It worked for me.
j
hm... let me then post my example
Screenshot from 2020-04-03 16-31-15.png
I would expect group1 to be included (with filtered options)
see the blue highlited lines
v
Post the whole code, I’ll try localy
j
Copy code
data class ScopeGroup(
    val id: String,
    val scopeOptions: List<ScopeOption>
)

data class ScopeOption(
    val id: String,
    val status: String
)

val optionList1 = listOf(
    ScopeOption("option 1-1", "1"),
    ScopeOption("option 1-2", "2"),
    ScopeOption("option 1-3", "3")
)
val optionList2 = listOf(
    ScopeOption("option 2-1", "1")
)
val group1 = ScopeGroup("group1", optionList1)
val group2 = ScopeGroup("group2", optionList2)

val groupList = listOf(
    group1,
    group2
)

val listOfOptions = groupList.flatMap { group -> group.scopeOptions.filter { option -> option.status == "1" } }

val listOfGroups = groupList.filter { group -> group.scopeOptions.all { option -> option.status == "1" } }
// TODO group2 is missing !?
What is working for me is:
Copy code
val result = groupList.map { ScopeGroup(it.id, it.scopeOptions.filter { option -> option.status == "1" }) }
n
I'm still not sure I understand the requirements. I thought you wanted to retain all scope groups which have at least one matching option? In that case, I would use
Copy code
groupList.filter { scopeGroup -> scopeGroup.scopeOptions.any { scopeOption -> scopeOption.status == "1"} }
v
@nkiesel what’s the difference here between
any{}
and
all{}
?
n
any
will include all scope groups that have at least one matching option.
all
will only include scope groups where all options are matching. Given
val l = listOf(listOf(1,2,3),listOf(2,2),listOf(2,2,3)
, `l.filter{ any { it == 2 }`will return al list of 3 sublists, but `l.filter { all it == 2}`will return a list of only the 2nd sub-list because that is the only one where all items are
2