Remove duplicates
I read a nice post about removing duplicates from a list, I must say that im stunned that i didn’t think about it. It’s so dead simple.
I post here so I can find it the next time I might need such a little trick
Comments
2 Responses to “Remove duplicates”
Leave a Reply
You might be better off with the following….
public void removeDuplicates(List items) {
Set set = new LinkedHashSet();
set.addAll(items);
items.removeAll();
items.addAll(set);
}
This way you’ll still have the same object reference that you started with instead of a completely new list.
Just be aware that this method does create a lot of objects behind the scenes. A LinkedHashSet uses a LinkedHashMap, which creates one entry (object) for every item added. Clearly these all need to be created and garbage collected. Depending on your setup that may be important. (Normally though, I would advise not worrying too much about object creation and gc
But for a smaller list it may be quicker to deduplicate without the Set.