Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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.

share|improve this question

marked as duplicate by Jeroen Vannevel, Luiggi Mendoza, Robin Green, Mario, Daniel Pinzon Dec 6 '13 at 22:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

10  
Add 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 : stackoverflow.com/questions/203984/… –  Alexis C. Dec 6 '13 at 21:16
    
any particular kind? I assume Set prevents duplicates? I'm a beginner. –  eggHunter Dec 6 '13 at 21:17
    
Refer to java.util.Set –  Luiggi Mendoza Dec 6 '13 at 21:19

3 Answers 3

Please use a Set which prevents storage of duplicates at the first place 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 );
    }
}

add() of the Set returns false if the element already exists.

Credits to @Aaron Digulla

share|improve this answer
2  
A HashSet prevents duplicates. –  Josh M Dec 6 '13 at 21:20
4  
@JoshM All sets should prevent duplicates. The first line from the javadoc is "A collection that contains no duplicate elements." –  Joshua Taylor Dec 6 '13 at 21:21
    
LinkedHashSet should be used in this case to items order –  patrz jaka franca Dec 6 '13 at 21:21
    
@JoshuaTaylor I thought a TreeSet still allowed duplicates. Oh, my bad, nvm you're right. –  Josh M Dec 6 '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 for more info. –  Luiggi Mendoza Dec 6 '13 at 21:42

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.

share|improve this answer

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

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.