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.
Asked
Active
Viewed 6.0k times
9
-
10Add all the objects of your arrayList in a Set (LinkedHashSet will maintain the order of the original list, otherwise HashSet will do it fine, just make sure that you override equals and hashcode for your class). See this : http://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist – Alexis C. Dec 06 '13 at 21:16
-
any particular kind? I assume Set prevents duplicates? I'm a beginner. – eggHunter Dec 06 '13 at 21:17
-
Refer to [`java.util.Set`](http://docs.oracle.com/javase/7/docs/api/java/util/Set.html) – Luiggi Mendoza Dec 06 '13 at 21:19
3 Answers
34
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

Maveňツ
- 1
- 12
- 50
- 89

e.doroskevic
- 2,129
- 18
- 25
-
2
-
4@JoshM _All_ sets should prevent duplicates. The first line from [the javadoc](http://docs.oracle.com/javase/7/docs/api/java/util/Set.html) is "A collection that contains no duplicate elements." – Joshua Taylor Dec 06 '13 at 21:21
-
-
@JoshuaTaylor I thought a `TreeSet` still allowed duplicates. Oh, my bad, nvm you're right. – Josh M Dec 06 '13 at 21:22
-
@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](http://stackoverflow.com/q/16931990/1065197) for more info. – Luiggi Mendoza Dec 06 '13 at 21:42
-
It seems strange that you would do it this way rather than what @ashes999 did. Both will work obviously but I think using a .contains() call is more clear as to what you're trying to achieve. – Kyle Bridenstine Jul 28 '16 at 17:52
9
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.
3
Hashset
will remove duplicates. Example:
Set< String > uniqueItems = new HashSet< String >();
uniqueItems.add("a");
uniqueItems.add("a");
uniqueItems.add("b");
uniqueItems.add("c");
The set "uniqueItems"
will contain the following : a, b, c

Nana Ghartey
- 7,901
- 1
- 24
- 26