https://kotlinlang.org logo
Title
o

oday

04/19/2019, 4:36 PM
hey guys, i have list A and list B, I want to go over B and set some property inside A depending on what I find in B, I wanna do this without 2 literal loops
s

snowe

04/19/2019, 4:39 PM
what do you mean 'set some property'?
o

oday

04/19/2019, 4:42 PM
like I get my own list A, then i get a list of the same type, Favorites (B), and now I want to apply an
is_favorite == true
on the items in list B that exist in List A
s

snowe

04/19/2019, 4:43 PM
well I don't think there's a way to 'know' what is in list b unless you iterate over it, so you're gonna have two loops no matter what.
o

oday

04/19/2019, 4:44 PM
i know, but I dont want to say
for (
s

snowe

04/19/2019, 4:46 PM
ah, you shouldn't be using for in kotlin anyway.
o

oday

04/19/2019, 4:46 PM
i wouldn’t go that far
lol
s

snowe

04/19/2019, 4:48 PM
without knowing what your lists actually look like I can only provide small help.
you could use
.aggregate
so group your
A
list and then
aggregate
to a
B
list
or you could use
associateWith
but I'm once again unsure of what your objects look like so I'm limited in what I can come up with. (I'm a very visual person 😂 )
o

oday

04/19/2019, 4:55 PM
here it is
searchResult.results.filter { carToMap ->
                carToMap.id == favoritesSearchResult.results.find { it.id == carToMap.id }?.id
            }.map { it.is_favorite = true }
s

snowe

04/19/2019, 8:53 PM
sorry, been very busy. let me look
oh is that your solution?
i

ilya.gorbunov

04/19/2019, 11:20 PM
You can collect favorites or favorite ids into set and then use
contains
of that set:
val favoriteIds = favoritesSearchResult.results.map { it.id }.toSet()
searchResult.results.forEach {
    it.is_favorite = it.id in favoriteIds
}
o

oday

04/20/2019, 9:42 AM
sure yes
of course you could do that