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!