Let’s say I have a List object and an iterator for that list.
Now I sort the list with java.util.Collections.sort()
- What happens to the iterator?
- Is its behavior still defined and can it still be used?
- If not, can I prevent destroying the iterators for the list?
I know, this problem could be circumvented by changing the program design, cloning the list for example, but I specificly want to know the ‘official’ behavior of Java.
Most of the collections in
java.utilare ‘fail-fast’ and may throw aConcurrentModificationExceptionif the underlying collection is changed. It should be pointed out that this is intended for debugging and so is not guaranteed. According to the javadocs, this is true of all decedents ofAbstractList, but this is not true ofCopyOnWriteArrayList, which is intended for multi-threaded use.