HashMap\'s javadoc states:
if the map is structurally modified at any time after the iterator is created, in any way except through the iterator\'s own re
If we will look into HashMap
sources and compare them between Java 6 and Java 7 we will see such interesting difference:
transient volatile int modCount;
in Java6 and just transient int modCount;
in Java7.
I'm sure that it is cause for different behavior of mentioned code due to this:
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
UPD: It seems to me, that this is a known Java 6/7 bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6625725 which was fixed in latest Java7.
UPD-2: Mr. @Renjith said, that he just tested and did not found any difference in behavior of HashMaps implementation. But I just tested it too.
My test was:
1) I have created HashMap2
class, which is absolutely copy of HashMap
from Java 6.
One important thing is we need to introduce here 2 new fields:
transient volatile Set keySet = null;
and
transient volatile Collection values = null;
2) Then I used this HashMap2
in test of this question and run it under Java 7
Result: it works like such test under Java 6
, i.e. there isn't any ConcurentModificationException
.
That all proves my conjecture. Q.E.D.