Suppose I am iterating over some collection, then call remove()
with absent key so that it does nothing. Will next iteration cause exception?
Asked
Active
Viewed 76 times
0

Suzan Cioc
- 29,281
- 63
- 213
- 385
-
1Have you tried it to find out? – Dave Jun 06 '12 at 11:56
-
1What happens when you try it out? – Jesper Jun 06 '12 at 11:56
1 Answers
4
First of all, the ConcurrentModificationException
is optional. A compliant container does not have to attempt to spot concurrent modifications at all.
However, most standard containers tend to do it.
I've just checked a couple of them in JDK7, and they only bump up the modification count once they have found the element.
Having said that, I would discourage you from relying on this behaviour.

NPE
- 486,780
- 108
- 951
- 1,012
-
1If you know you're removing an absent key, you deliberately do a no-op. If you don't know, then you deliberately court a `ConcurrentModificationException`. In both cases, you're doing something silly or wrong. – Louis Wasserman Jun 06 '12 at 14:23