I should have pointed out that most other forms of modifying a collection while iterating over it result in a java.util.ConcurrentModifi
To avoid that (other than following the technique I have outlined) you would have to iterate over a copy of the collection and remove the elements from the original collection.
Main Topics
Browse All Topics





by: rpnmanPosted on 2009-10-04 at 09:24:19ID: 25489629
The code I've pasted below shows how there is a safe way to remove elements while iterating.
I've created a Foo containing only a string and an int that can be sorted using Comparator objects which I've also defined.
Using comparator objects is more flexible than implementing Comprable<Foo>, as this implementation shows.
There are three methods showing removal of duplicate members while iterating through the list -
removeDups only removes full duplicates.
removeDupStrings removes those whose strings are dups regardless of the int valule
removeDupInts removes dup ints regardless of string.
Look at the output, (documented in a comment at the beginning of the code) and you'll notice that the elements have been sorted in the list each time ...
Select allOpen in new window