Hi! Started working with kotlin in prod but still ...
# announcements
v
Hi! Started working with kotlin in prod but still not sure if i'm doing that right/performatc way. Suppose I have a User collection and a Pair<UserId,Tag> collection, Each User object has a String collection property where I have to associate the given Tags owned by this user. In other words, a typical OneToMany join. Would this be the best way? How could I use parallelism in the outer loop (since the operations wont mix up cause every tag has its own user)?
Copy code
val users = getUsers().apply {
        val tags = <get list of Pair<userid, tag>>

        forEach { user ->
            user.tags.addAll(tags.filter { it.first == user.id }.map { it.second })
        }
    }
k
This should be more performant.
Yours was
O(#users * #tags)
while this one is
O(#users + #tags)
🙏 1
v
yes, also i would have to change ´´apply´´ to ´´run´´ and return ´´ids.values´´
k
I don't see why that's needed
v
because with apply, the users val is receiving the getUsers, without the transformation made inside. right?
k
But it's not being transformed right? The users have a mutable collection.
s
Copy code
val userTags = getTags().groupBy(
      keySelector = { it.first },
      valueTransform = { it.second }
   ).withDefault { emptyList() }
   val users = getUsers().forEach { user ->
      user.tags += userTags.getValue(user.id)
   }