Issue
How can I use distinctBy
on a list of custom objects to strip out the duplicates? I want to determine "uniqueness" by multiple properties of the object, but not all of them.
I was hoping something like this would work, but no luck:
val uniqueObjects = myObjectList.distinctBy { it.myField, it.myOtherField }
Edit: I'm curious how to use distinctBy
with any number of properties, not just two like in my example above.
Solution
You can create a pair:
myObjectList.distinctBy { Pair(it.myField, it.myOtherField) }
The distinctBy
will use equality of Pair
to determine uniqueness.
Answered By - nhaarman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.