9

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
  • 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/… Commented Dec 6, 2013 at 21:16
  • any particular kind? I assume Set prevents duplicates? I'm a beginner. Commented Dec 6, 2013 at 21:17
  • Refer to java.util.Set Commented Dec 6, 2013 at 21:19

3 Answers 3

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

Set
Java Data Structures

Sign up to request clarification or add additional context in comments.

6 Comments

A HashSet prevents duplicates.
@JoshM All sets should prevent duplicates. The first line from the javadoc is "A collection that contains no duplicate elements."
LinkedHashSet should be used in this case to items order
@JoshuaTaylor I thought a TreeSet still allowed duplicates. Oh, my bad, nvm you're right.
@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.
|
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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.