Tagged articles
31 articles
Page 1 of 1
Sohu Smart Platform Tech Team
Sohu Smart Platform Tech Team
Feb 11, 2026 · Backend Development

Why TreeSet Throws ConcurrentModificationException and How to Fix It

An in‑depth look at why Java’s TreeSet can trigger ConcurrentModificationException under high concurrency, exploring the red‑black tree’s non‑atomic operations, thread scheduling nuances, and how various concurrent set implementations—synchronized wrappers, read‑write locks, ConcurrentSkipListSet, CopyOnWriteArraySet, and custom designs—compare in performance and suitability.

CollectionsConcurrentModificationExceptionJava
0 likes · 16 min read
Why TreeSet Throws ConcurrentModificationException and How to Fix It
JakartaEE China Community
JakartaEE China Community
Oct 20, 2025 · Fundamentals

How to Prevent ConcurrentModificationException in Java Collections

The article explains why ConcurrentModificationException occurs when a Java collection is structurally modified during iteration, illustrates typical scenarios such as enhanced‑for loops and multithreaded access, and provides practical solutions including iterator.remove(), thread‑safe collections, synchronized blocks and iterating over copies.

CollectionsConcurrentHashMapConcurrentModificationException
0 likes · 7 min read
How to Prevent ConcurrentModificationException in Java Collections
Architecture Digest
Architecture Digest
Mar 18, 2025 · Fundamentals

Understanding Java foreach Loop, HashMap Iteration, and ConcurrentModificationException

This article explains how Java's foreach loop is implemented using iterators, demonstrates the bytecode differences between array and collection traversal, analyzes why modifying a HashMap during foreach can trigger ConcurrentModificationException, and shows the correct way to safely modify collections with an iterator.

CollectionsConcurrentModificationExceptionHashMap
0 likes · 11 min read
Understanding Java foreach Loop, HashMap Iteration, and ConcurrentModificationException
Cognitive Technology Team
Cognitive Technology Team
Jun 23, 2024 · Fundamentals

Understanding Fail-Fast Iterators and ConcurrentModificationException in Java Collections

The article explains why fail‑fast iterators throw ConcurrentModificationException when a collection is modified during iteration, outlines two common scenarios that trigger the exception, and presents both incorrect and correct Java code examples along with strategies such as avoiding modifications, using synchronization, iterator.remove(), or switching to fail‑safe concurrent collections.

CollectionsConcurrentModificationExceptionFail-Fast Iterator
0 likes · 5 min read
Understanding Fail-Fast Iterators and ConcurrentModificationException in Java Collections
Architect's Guide
Architect's Guide
Jun 2, 2024 · Fundamentals

Pitfalls and Best Practices of Arrays.asList and ArrayList.subList in Java

This article explains how Java's Arrays.asList creates a fixed‑size list that throws UnsupportedOperationException on add/remove, and how ArrayList.subList returns a mutable view whose non‑structural changes affect both the original list and the sublist while structural changes can trigger ConcurrentModificationException, providing code examples and practical guidelines.

Arrays.asListCollectionsConcurrentModificationException
0 likes · 9 min read
Pitfalls and Best Practices of Arrays.asList and ArrayList.subList in Java
IT Services Circle
IT Services Circle
Jan 10, 2024 · Backend Development

Why Using forEach to Remove Elements from an ArrayList Throws ConcurrentModificationException and How to Delete Safely

The article explains why iterating an ArrayList with forEach and removing elements causes a ConcurrentModificationException, analyzes the underlying fail‑fast iterator behavior, and presents three correct ways—using Iterator.remove, removeIf, and collecting then removing—to delete elements safely.

ArrayListConcurrentModificationExceptionIterator
0 likes · 4 min read
Why Using forEach to Remove Elements from an ArrayList Throws ConcurrentModificationException and How to Delete Safely
Java Architect Essentials
Java Architect Essentials
Mar 19, 2023 · Backend Development

Why Arrays.asList and subList Can Throw Unexpected Exceptions in Java

This article explains the hidden pitfalls of Java's Arrays.asList and ArrayList.subList methods, showing how fixed‑size lists cause UnsupportedOperationException, how subList creates a mutable view that can trigger ConcurrentModificationException, and provides concrete code examples and source‑code analysis.

Arrays.asListConcurrentModificationExceptionJava
0 likes · 10 min read
Why Arrays.asList and subList Can Throw Unexpected Exceptions in Java
macrozheng
macrozheng
Feb 20, 2023 · Fundamentals

8 Reliable Ways to Remove Elements from a Java List (And Why Some Fail)

This article examines eight common techniques for deleting elements from a Java List, explains why certain loop‑based approaches cause errors or unexpected results, and highlights the three reliable methods—including reverse for‑loops, iterator.remove, and Stream filtering—that work correctly.

/loopCollectionConcurrentModificationException
0 likes · 9 min read
8 Reliable Ways to Remove Elements from a Java List (And Why Some Fail)
Java Backend Technology
Java Backend Technology
Jun 28, 2022 · Backend Development

Why ArrayList.subList Can Leak Memory, Loop Forever, and Crash Your Java App

This article explains how improper use of Java's ArrayList.subList can cause hidden memory leaks, infinite loops, ConcurrentModificationExceptions, and serialization failures in RPC frameworks, and provides concrete best‑practice solutions such as copying to a new list or using stream operations.

ArrayListConcurrentModificationExceptionJava
0 likes · 9 min read
Why ArrayList.subList Can Leak Memory, Loop Forever, and Crash Your Java App
Programmer DD
Programmer DD
May 9, 2022 · Fundamentals

Can You Remove Items Inside a Java foreach Loop? Why It Fails and How to Fix It

This article explores how Java's foreach loop handles collection traversal, demonstrates why removing or modifying elements directly within a foreach causes ConcurrentModificationException, compares it with traditional for loops, and shows the correct way to delete or update elements using an iterator or element properties.

/loopCollectionConcurrentModificationException
0 likes · 9 min read
Can You Remove Items Inside a Java foreach Loop? Why It Fails and How to Fix It
Cognitive Technology Team
Cognitive Technology Team
Apr 4, 2022 · Fundamentals

Why Removing Elements from a HashMap During a For‑Each Loop Causes ConcurrentModificationException and How to Fix It

Iterating a HashMap with a for‑each loop while calling its remove method triggers a ConcurrentModificationException because the loop uses an internal iterator that becomes inconsistent with the map’s modCount, and the article explains the cause, shows decompiled code, and provides correct removal approaches using the iterator or removeIf.

CollectionsConcurrentModificationExceptionHashMap
0 likes · 6 min read
Why Removing Elements from a HashMap During a For‑Each Loop Causes ConcurrentModificationException and How to Fix It
Programmer DD
Programmer DD
Oct 27, 2021 · Backend Development

Why Removing Elements from a Java List Often Fails and How to Fix It

This article explains common pitfalls when deleting elements from a Java List—such as index shifting, ConcurrentModificationException in foreach loops, and the difference between removing by index versus by object—and provides reliable solutions using index adjustment, reverse iteration, and Iterator.remove().

ConcurrentModificationExceptionIteratorJava
0 likes · 8 min read
Why Removing Elements from a Java List Often Fails and How to Fix It
Programmer DD
Programmer DD
Oct 24, 2021 · Fundamentals

Why Adding or Removing Elements Inside a Java foreach Loop Fails

This article explains why modifying a collection during a Java foreach loop triggers a ConcurrentModificationException, explores the underlying fail‑fast mechanism in ArrayList, and demonstrates safe alternatives such as using an Iterator's remove method or a CopyOnWriteArrayList.

ArrayListConcurrentModificationExceptionIterator
0 likes · 9 min read
Why Adding or Removing Elements Inside a Java foreach Loop Fails
Selected Java Interview Questions
Selected Java Interview Questions
Sep 30, 2021 · Fundamentals

Understanding the Role of modCount in Java HashMap's Fail‑Fast Mechanism

The article clarifies that the modCount field in java.util.HashMap is used to implement the fail‑fast iterator behavior, explains why the field was volatile in early JDK versions but not in later ones, and shows that ConcurrentModificationException can be thrown even in single‑threaded scenarios when the iterator detects structural changes.

ConcurrentModificationExceptionHashMapJDK
0 likes · 5 min read
Understanding the Role of modCount in Java HashMap's Fail‑Fast Mechanism
Selected Java Interview Questions
Selected Java Interview Questions
Jun 4, 2021 · Fundamentals

Why Removing Elements During forEach on an ArrayList Triggers ConcurrentModificationException and How to Fix It

The article explains that deleting elements from an ArrayList inside a forEach loop can cause a ConcurrentModificationException because the iterator's expected modification count diverges from the actual count, and it shows correct ways to remove items safely using an explicit Iterator or a CopyOnWriteArrayList.

ArrayListConcurrentModificationExceptionCopyOnWriteArrayList
0 likes · 5 min read
Why Removing Elements During forEach on an ArrayList Triggers ConcurrentModificationException and How to Fix It
Java Backend Technology
Java Backend Technology
Jun 1, 2021 · Fundamentals

Why Arrays.asList and subList Can Surprise You: Hidden Pitfalls

This article examines common pitfalls when using Java's Arrays.asList and ArrayList.subList methods, explaining why add operations throw UnsupportedOperationException, how subList creates a view that links modifications between original and sublists, and offers best‑practice recommendations to avoid runtime errors.

ArrayListArrays.asListCollections
0 likes · 9 min read
Why Arrays.asList and subList Can Surprise You: Hidden Pitfalls
Top Architect
Top Architect
Mar 29, 2021 · Fundamentals

Pitfalls of Using Arrays.asList and ArrayList.subList in Java

This article explains common traps when using Java's Arrays.asList and ArrayList.subList methods, including why add operations throw UnsupportedOperationException, how subList creates a view that shares modifications with the original list, and the circumstances that lead to ConcurrentModificationException.

Arrays.asListConcurrentModificationExceptionUnsupportedOperationException
0 likes · 8 min read
Pitfalls of Using Arrays.asList and ArrayList.subList in Java
Java Captain
Java Captain
Mar 27, 2021 · Fundamentals

Pitfalls of Using Arrays.asList and ArrayList.subList in Java

This article explains common pitfalls when using Java's Arrays.asList and ArrayList.subList methods, illustrating why add operations cause UnsupportedOperationException, how subList creates a view that shares modifications with the original list, and provides best‑practice recommendations to avoid runtime errors.

Arrays.asListConcurrentModificationExceptionJava
0 likes · 8 min read
Pitfalls of Using Arrays.asList and ArrayList.subList in Java
Java Backend Technology
Java Backend Technology
Jan 24, 2021 · Fundamentals

Why Arrays.asList and subList Can Throw Unexpected Exceptions in Java

This article explains common pitfalls when using Java's Arrays.asList and ArrayList.subList methods, illustrating why add operations may trigger UnsupportedOperationException or ConcurrentModificationException, and provides guidance on safe usage to avoid runtime errors in production.

Arrays.asListCollectionsConcurrentModificationException
0 likes · 10 min read
Why Arrays.asList and subList Can Throw Unexpected Exceptions in Java
macrozheng
macrozheng
Jun 15, 2020 · Backend Development

Avoid ConcurrentModificationException: Safe Ways to Remove Elements While Iterating in Java

This article explains why removing items from a Java List inside a foreach loop triggers java.util.ConcurrentModificationException and demonstrates three reliable alternatives—using Iterator.remove(), a forward for‑loop with index adjustment, and a reverse for‑loop—to safely modify collections during iteration.

ConcurrentModificationExceptionIteratorJava
0 likes · 7 min read
Avoid ConcurrentModificationException: Safe Ways to Remove Elements While Iterating in Java
Java Backend Technology
Java Backend Technology
Jun 13, 2020 · Fundamentals

Why Does foreach Throw ConcurrentModificationException and How to Fix It?

During a Java interview, a common mistake of using a foreach loop to remove elements from an ArrayList triggers a ConcurrentModificationException; this article explains the underlying iterator mechanism, shows the bytecode behavior, and presents four safe alternatives—including iterator.remove(), forward and backward for-loops—to avoid the error.

ArrayListConcurrentModificationExceptionIterator
0 likes · 7 min read
Why Does foreach Throw ConcurrentModificationException and How to Fix It?
Architect's Tech Stack
Architect's Tech Stack
Jun 7, 2020 · Backend Development

How to Avoid java.util.ConcurrentModificationException When Removing Elements from a List

This article explains why a foreach loop over an ArrayList can throw java.util.ConcurrentModificationException when removing elements, analyzes the underlying iterator mechanism, and presents three safe alternatives—using Iterator.remove(), a forward-index for loop with index correction, and a reverse for loop—to modify collections without errors.

/loopArrayListConcurrentModificationException
0 likes · 7 min read
How to Avoid java.util.ConcurrentModificationException When Removing Elements from a List
Selected Java Interview Questions
Selected Java Interview Questions
Apr 27, 2020 · Fundamentals

How to Remove Elements from a List Without Causing java.util.ConcurrentModificationException

This article explains why removing items from a Java List inside a foreach loop triggers a ConcurrentModificationException and demonstrates three correct approaches—using Iterator.remove(), a forward indexed for loop with index correction, and a reverse for loop—each with full code examples and output.

ArrayListConcurrentModificationExceptionIterator
0 likes · 6 min read
How to Remove Elements from a List Without Causing java.util.ConcurrentModificationException
Java Backend Technology
Java Backend Technology
Mar 23, 2020 · Fundamentals

Why Removing Elements in a Java foreach Loop Can Crash Your Code

This article explains why using a foreach loop to remove items from a Java ArrayList can trigger a ConcurrentModificationException, analyzes the underlying iterator mechanism, and shows the correct way to modify collections safely with an explicit Iterator.

ArrayListCollectionsConcurrentModificationException
0 likes · 5 min read
Why Removing Elements in a Java foreach Loop Can Crash Your Code
Qunar Tech Salon
Qunar Tech Salon
Jun 3, 2015 · Fundamentals

Understanding HashMap ConcurrentModificationException and Using ConcurrentHashMap in Java

This article explains why iterating and removing entries from a HashMap throws ConcurrentModificationException, describes the internal iterator mechanism, and provides correct solutions using an Iterator or replacing HashMap with ConcurrentHashMap, including detailed explanations of ConcurrentHashMap's structure and its get, put, remove, size, and iteration implementations.

CollectionsConcurrentHashMapConcurrentModificationException
0 likes · 11 min read
Understanding HashMap ConcurrentModificationException and Using ConcurrentHashMap in Java