Companion code for the ankurm.com post "Top 40 Java Collections Interview Questions (HashMap Internals to Fail-Fast Iterators)". Verified against JDK 25's own HashMap source (TREEIFY_THRESHOLD=8, UNTREEIFY_THRESHOLD=6, MIN_TREEIFY_CAPACITY=64), with every claim backed by a committed docs/output/*.txt transcript from a real run. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YXCrLgRKFgCh9RHKW8xaqJ
13 lines
713 B
Plaintext
13 lines
713 B
Plaintext
The fail-fast contract is 'best-effort', not guaranteed: a case that does NOT throw
|
|
===================================================================================
|
|
|
|
list before: [10, 20] (size=2)
|
|
elements the loop actually saw before ending: [10]
|
|
list after the loop: [20]
|
|
ConcurrentModificationException thrown: false
|
|
|
|
Why: after removing 10, size becomes 1 and the iterator's cursor is already 1
|
|
(it advanced to 1 when next() returned 10). ArrayList.Itr.hasNext() is just
|
|
'return cursor != size;' - no modCount check - so hasNext() sees 1 != 1, returns
|
|
false, and the loop ends normally. next() is the only method that checks
|
|
modCount, and it is never called again. The element 20 is silently never visited. |