DEV Community

loading...

Discussion on: Java Data Structures

Collapse
martinhaeusler profile image
Martin Häusler

A nice summary!

One small comment: a ConcurrentModificationException does not only occur when two threads access the same collection. It may also happen that your own (single-threaded) code iterates over a collection, and makes changes to it during the iteration.

For example:

for(Object element : collection){
    collection.remove(element);
}

This snippet will always throw a ConcurrentModificationException (except for empty collections) even though only a single thread is at work. We are mutating the collection while an iterator is still active. Maybe specialized collections, such as the CopyOnWriteArrayList, can survive this loop without throwing an exception, but the general Collection contract specifies that this is an invalid case.

Collapse
vrnsky profile image
Yegor Voronyansky Author

Thank you, I am update the post