# Java ConcurrentModificationException

**URL:** https://boards.straightdope.com/t/java-concurrentmodificationexception/528841
**Category:** Factual Questions
**Created:** [February 13, 2010, 8:08am UTC](https://boards.straightdope.com/t/java-concurrentmodificationexception/528841 "2010-02-13T08:08:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Lobot](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lobot](https://boards.straightdope.com/u/Lobot)
#### Post date: [February 13, 2010, 8:08am UTC](https://boards.straightdope.com/t/java-concurrentmodificationexception/528841/1 "2010-02-13T08:08:49Z")

</div>

OK, I’m not a Java guru, but I have no idea as to what’s going wrong here. Whatever it is, it looks like I’m just misunderstanding something.

```auto

Iterator itr = lhm.keySet().iterator();
while (itr.hasNext()) {
    int[] foo = (int[]) itr.next();
    ...
}

```

…where lhm is a LinkedHashMap whose keys are arrays of ints.

Once we get to the cast, the following exception occurs:

```auto

Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:390)
	at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:401)

```

So what am I doing wrong?

ETA: There is only one thread running, FWIW.

---

<div class="post-metadata">

### Author: ![ticker](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@ticker](https://boards.straightdope.com/u/ticker)
#### Post date: [February 13, 2010, 9:43am UTC](https://boards.straightdope.com/t/java-concurrentmodificationexception/528841/2 "2010-02-13T09:43:18Z")

</div>

> [@](#):
>
> The iterators returned by the iterator method of the collections returned by all of this class’s collection view methods are fail-fast:\*\* if the map is structurally modified at any time after the iterator is created\*\*, in any way except through the iterator’s own remove method, the iterator will throw a _ConcurrentModificationException_. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

My guess is that within your ‘while’ loop you are somehow messing with the map.

---

<div class="post-metadata">

### Author: ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)
#### Post date: [February 13, 2010, 9:51am UTC](https://boards.straightdope.com/t/java-concurrentmodificationexception/528841/3 "2010-02-13T09:51:06Z")

</div>

My guess? It’s in the line “…”.

**Indistinguishable** ’s law of debugging: it’s always in the part you were sure was irrelevant

---

<div class="post-metadata">

### Author: ![Lobot](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lobot](https://boards.straightdope.com/u/Lobot)
#### Post date: [February 13, 2010, 9:53am UTC](https://boards.straightdope.com/t/java-concurrentmodificationexception/528841/4 "2010-02-13T09:53:17Z")

</div>

Bingo. I was conditionally removing elements. Once I used itr.remove(), it fixed itself.

Thanks!

---

<div class="post-metadata">

### Author: ![Lobot](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lobot](https://boards.straightdope.com/u/Lobot)
#### Post date: [February 13, 2010, 9:54am UTC](https://boards.straightdope.com/t/java-concurrentmodificationexception/528841/5 "2010-02-13T09:54:36Z")

</div>

> [@Indistinguishable](#):
>
> My guess? It’s in the line “…”.
> 
> **Indistinguishable** ’s law of debugging: it’s always in the part you were sure was irrelevant

Your law is worth remembering…
