How to avoid "ConcurrentModificationException" while removing elements from ArrayList
while iterating it?
Dodge the ConcurrentModificationException
using Iterator
or ListIterator
:
Prefer back-and-forth access? Say hi to ListIterator
:
In Java 8 and higher, befriend the removeIf
function:
Making the changes during iteration ensures safety and no exception stress!
Delving deeper: Let your fingers do the coding
The classic loop: Reverse for to the rescue
Doing list.remove(item)
within a forward for
loop is a no-no; it's like stepping on a rake. Instead, go backwards:
Removing from the back won't shift the elements you haven't iterated yet.
Guard your ArrayList in a multi-thread environment
If your ArrayList
is a field in a multi-threaded class, synchronize your code or face the „music“ (also known as the exception we're discussing):
The Army Knife for concurrency: CopyOnWriteArrayList
In a multithreaded context, makeCopyOnWriteArrayList
your buddy:
But remember, this is only for frequent-reads, infrequent-writes. Otherwise, prepare for a performance hit.
Tweak for special cases
Need more performance in your concurrent setup? Use either Collections.synchronizedList
or ConcurrentHashMap
—your call!
Tips, tricks, and when to use what
Is it a bird, is it a plane, it's batch removal with removeAll!
Initializing ArrayList with values
Want to preload some values into your list? Here you go:
Deciphering the mystery of Exceptions
When your console lights up with red, it's time to unseal the scroll of stack trace. Fear not, your key is the method just before the Exception.
Don't just code, code smartly
Iterate with caution using for-each
While for-each loop is handy, it can lead you to a trap. For any modification plans, stick to iterators or streams.
CopyOnWriteArrayList
is handy for lots of reads and sparse writes. Certainly, not a good choice for everyday, single-threaded tasks.
The removeIf
function is your friend if the situation involves predicates. It's like an expressway to clean code:
Lastly, remember that the size of the collection and the cost of the conditional check determines the performance of the method chosen.
Was this article helpful?