I want to delete duplicate elements and therefore iterate through a ArrayList and compare two consecutive elements. (Persons are comparable)
ArrayList<Person> persons = getHelper().findAllPersons();
Collections.sort(persons);
ListIterator<Person> it = persons.listIterator();
if(it.hasNext()) {
Person tmp = it.next();
while(it.hasNext()) {
if(tmp.getLastDiscovered() == it.next().getLastDiscovered()) {
getHelper().delete(tmp);
}
tmp = it.next();
}
}
I get a NoSuchElementException at tmp = it.next();
Shouldn't the while(it.hasNext()) prevent that?
final Set<Person> unqiuePeople = new TreeSet<Person>(persons)will do what you want in one line.Collections.sortto order the items I assume this is already defined.