5

I have an ArrayList with custom objects. What i want is to remove the duplicates from the array depending on the name property of the custom object. I have tried to accomplish this with Set person = new TreeSet(); but it's not working. I guess because the set is comparing addresses or something else than the name property. So i'm now trying to use an iterator which is not removing the duplicates either. This is what i got;

ArrayList<Person> people = new ArrayList<Person>();
Iterator<Person> iterator = people.iterator();
while (iterator.hasNext()) {
   Person person = iterator.next();

   if (person.getName().equals(iterator.next().getName())) {
       iterator.remove();
   }
}
for (Person person : people) {
    System.out.println(person.getName());
}

The ArrayList is not being modified though i see duplicates in it. I need some help. Thanks!

3
  • does person implements equals and hashcode? Set should work if you implement equals and hashcode Commented May 22, 2013 at 11:56
  • Generally a bad idea to remove whilst iterating... perhaps better to create a separate list and filter (add) the intended results into the new list. Commented May 22, 2013 at 11:59
  • why is it a bad idea to remove while iterating? Its a great idea if access it from one thread only. If its not you have other issues Commented May 22, 2013 at 12:23

4 Answers 4

10

I had the same situation, and I came up with this solution to use SortedSet. In this case, those objects which cause set's comparator to return 0, will only be inserted once in the Set.

Here is an example:

SortedSet<Person> persons = new TreeSet<Person>(new Comparator<Person>() {
    @Override
    public int compare(Person arg0, Person arg1) {
        return arg0.getName().compareTo(arg1.getName());
    }
});

And now if you insert a Person into your persons, the duplicates (based on their name property) will not be inserted.

So you can iterator over your list<Person> and insert every item of it into your persons set, and be sure that you will not have any duplicates. So the rest would be like:

Iterator<Person> iterator = people.iterator();
while(iterator.hasNext()) {
    persons.add(iterator.next());
}
people.clear();
people.addAll(persons); //Now, your people does not contain duplicate names
Sign up to request clarification or add additional context in comments.

3 Comments

I implemented Comparable and Comparator in my Person class and it works great. Thanks a lot
Hey.. I have done this and worked successfully. But my original list with the duplicate values also changes to the sorted value. Don't know why. Please help me.
Most probably, instead of making a copy of your list, you put them right into your sorted-set. It might sound a little trivial, but it is better for you to have a for-loop and copy every single element into your new set, to preserve the first list of yours.
2

Your code is currently broken because you only compare objects with the next object in the list. To correct your current approach, you would need to have another sub-loop which compares each object with all others in the list. That might get messy with nested iterators.

One alternative is to define a new list and populate it with items once you've confirmed they are not duplicates. This avoids nesting iterators.

Finally, another option would be to define an equals method that compares based on this property and throw the objects in a Set. Don't forget hashCode too.

1 Comment

compare objects with the next object In addition, the next object won't compare with the third object. The fourth object won't compare with the fifth object ....
2

Its not removing because you are only comparing each element with the next element. You can store the names in a HashSet which can hold only 1 of each String then remove the item if its name is already in the set.

HashSet<String> seen = new HashSet<String>();
while (iterator.hasNext()) {
     Person p = iterator.next();
     if (seen.contains(p.getName())) {
           iterator.remove();
     } else { 
           seen.add(p.getName());
     }
}

Comments

0

Everytime you write .next(), the iteration is taken 1 step forward. So, say you have 10 person in your list. You pick the 1st person, and check the next person using iterator.next(). Though you fetch the 2nd person, the iterator is now at person 2. So in the next run, the 3rd person is fetched and compared with the 4th person.

What you ought to do is, pick 1 person and compare his name with the names of all the 10 person in the list and then remove all the instances of duplicate objects from the list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.