#1: ConcurrentModificationException Getting a ConcurrentModificationException when trying to modify (i.e. adding or removing an item) a collection while iterating. The following code throws a ConcurrentModificationException.
1 2 3 4 5 6 7 | List<T> list = getListOfItems(); for (Iterator<T> iter = list.iterator(); iter.hasNext(); ) { T obj = iter.next(); if (obj.someCondition()) { list.remove(0); // ConcurrentModificationException } } |
To avoid ConcurrentModificationException in a single-threaded environment, you can remove the object that you…