I have a very lengthy ArrayList comprised of objects some of them however, are undoubtedly duplicates. What is the best way of finding and removing these duplicates. Note: I have written a boolean-returning compareObjects() method.
3 Answers
Example
List<Item> result = new ArrayList<Item>();
Set<String> titles = new HashSet<String>();
for( Item item : originalList ) {
if( titles.add( item.getTitle() )) {
result.add( item );
}
}
Reference
6 Comments
Josh M
A
HashSet prevents duplicates.Joshua Taylor
@JoshM All sets should prevent duplicates. The first line from the javadoc is "A collection that contains no duplicate elements."
homik
LinkedHashSet should be used in this case to items order
Josh M
@JoshuaTaylor I thought a
TreeSet still allowed duplicates. Oh, my bad, nvm you're right.Luiggi Mendoza
@JoshM the differente between a common
Set e.g. HashSet and LinkedHashSet and a SortedSet e.g. TreeSet is that Set use equals and hashCode methods to compare object equality while SortedSet use compareTo or a Comparator for its elements. See here for more info. |
You mentioned writing a compareObjects method. Actually, you should override the equals method to return true when two objects are equal.
Having said that, I would just return a new list that contains unique elements from the original:
ArrayList<T> original = ...
List<T> uniques = new ArrayList<T>();
for (T element : original) {
if (!uniques.contains(element)) {
uniques.add(element);
}
}
This only works if you override equals. See this question for more information.
java.util.Set