Skip to content
Advertisement

Combine two lists by ID

I have two lists of objects with contents that look like this:

JavaScript

I can’t come up with anything good to “merge” these two lists overriding the nulls. The ideal end result would be:

JavaScript

I’ve been looking into Kotlin collections to see if there’s anything that would help me get closer to this but I have only been able to do this:

JavaScript

This returns a map with the ID as key and the full object as value… but I still need to merge the values into one object.

Is there anything that will do this that is not a very manual approach of checking nulls and picking the value in the other list?

Advertisement

Answer

I made the following (using “User” as a placeholder. Replace it with your own data structure):

JavaScript

And this is the “User” class (Replace it with your own Data structure):

JavaScript

The result I got was:

JavaScript

So I guess it works

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement