Vitali Plagov
04/03/2020, 8:41 AMdata class ScopeGroup(
val id: UUID,
val scopeOptions: List<ScopeOption>
)
and
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:
list.flatMap { group -> group.scopeOptions.filter { option -> option.status == "ACTIVE" } }
But it returns List<ScopeOption>
. Is it possible to return List<ScopeGroup>
instead?Michael de Kaste
04/03/2020, 8:42 AMVitali Plagov
04/03/2020, 8:44 AMMichael de Kaste
04/03/2020, 8:45 AMMichael de Kaste
04/03/2020, 8:46 AMVitali Plagov
04/03/2020, 8:46 AMI want to only get my scopegroups where ALL scopeotions are activeI want exactly this.
Michael de Kaste
04/03/2020, 8:47 AMVitali Plagov
04/03/2020, 8:47 AMJörg Winter
04/03/2020, 2:24 PMList<ScopeGroup>
, in which each ScopeGroup only has scopeOptions with status == "ACTIVE" ?Jörg Winter
04/03/2020, 2:28 PMVitali Plagov
04/03/2020, 2:29 PMJörg Winter
04/03/2020, 2:31 PMJörg Winter
04/03/2020, 2:31 PMJörg Winter
04/03/2020, 2:32 PMJörg Winter
04/03/2020, 2:32 PMVitali Plagov
04/03/2020, 2:37 PMJörg Winter
04/03/2020, 2:50 PMdata 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 !?
Jörg Winter
04/03/2020, 2:57 PMval result = groupList.map { ScopeGroup(it.id, it.scopeOptions.filter { option -> option.status == "1" }) }
nkiesel
04/03/2020, 7:16 PMgroupList.filter { scopeGroup -> scopeGroup.scopeOptions.any { scopeOption -> scopeOption.status == "1"} }
Vitali Plagov
04/04/2020, 9:22 AMany{}
and all{}
?nkiesel
04/06/2020, 5:58 PMany
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