hey guys, i have list A and list B, I want to go o...
# getting-started
o
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
what do you mean 'set some property'?
o
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
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
i know, but I dont want to say
for (
s
ah, you shouldn't be using for in kotlin anyway.
o
i wouldn’t go that far
lol
s
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
here it is
Copy code
searchResult.results.filter { carToMap ->
                carToMap.id == favoritesSearchResult.results.find { it.id == carToMap.id }?.id
            }.map { it.is_favorite = true }
s
sorry, been very busy. let me look
oh is that your solution?
i
You can collect favorites or favorite ids into set and then use
contains
of that set:
Copy code
val favoriteIds = favoritesSearchResult.results.map { it.id }.toSet()
searchResult.results.forEach {
    it.is_favorite = it.id in favoriteIds
}
o
sure yes
of course you could do that