Add collections module: HashMap bucket layout, treeification, and a real ConcurrentModificationException reproduction

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
This commit is contained in:
2026-10-01 04:27:27 +00:00
co-authored by Claude Sonnet 5
commit d4b3a63615
27 changed files with 1085 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
#!/usr/bin/env bash
# Regenerates every file in docs/output/ from scratch. Run from the module root
# (java-interview-lab/collections).
#
# Needs JDK 25 (LTS) on PATH as `java`/`javac`, and a `mvn` that can reach Maven Central the
# first time (surefire/compiler plugins must be cached locally before -o works).
set -eu
cd "$(dirname "$0")/.."
echo "== compiling =="
mvn -q -B compile
echo "== 01/02: reflection into HashMap internals, with and without --add-opens =="
mkdir -p docs/output
java -cp target/classes com.ankurm.interviewlab.collections.ReflectionAddOpensDemo \
> docs/output/01-reflection-without-add-opens.txt 2>&1 || true
java --add-opens java.base/java.util=ALL-UNNAMED -cp target/classes \
com.ankurm.interviewlab.collections.ReflectionAddOpensDemo \
> docs/output/02-reflection-with-add-opens.txt 2>&1
# Strip the sandbox's own proxy/trust-store banner (JAVA_TOOL_OPTIONS) - it is environment
# noise, not part of either demonstration, and should not be in a committed transcript.
for f in docs/output/01-reflection-without-add-opens.txt docs/output/02-reflection-with-add-opens.txt; do
grep -v '^Picked up JAVA_TOOL_OPTIONS' "$f" > "$f.tmp" && mv "$f.tmp" "$f"
done
echo "== 03-10: JUnit-driven transcripts (BucketLayoutTest, TreeifyTriggerTest, ConcurrentModificationTest, EqualsHashCodeContractTest) =="
mvn -q -B test
echo "== done - docs/output now contains: =="
ls -1 docs/output
+32
View File
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
# Runs one named scenario and prints it to stdout (does not touch docs/output - use
# run-all.sh to regenerate the committed transcripts). Examples:
#
# ./scripts/run.sh add-opens-fail # reflection WITHOUT --add-opens (crashes on purpose)
# ./scripts/run.sh add-opens-ok # reflection WITH --add-opens
# ./scripts/run.sh treeify # run just the treeification test, verbose
# ./scripts/run.sh cme # run just the ConcurrentModificationException tests
set -eu
cd "$(dirname "$0")/.."
scenario="${1:?usage: run.sh <add-opens-fail|add-opens-ok|treeify|cme|eqhash|bucket>}"
mvn -q -B compile
case "$scenario" in
add-opens-fail)
java -cp target/classes com.ankurm.interviewlab.collections.ReflectionAddOpensDemo ;;
add-opens-ok)
java --add-opens java.base/java.util=ALL-UNNAMED -cp target/classes \
com.ankurm.interviewlab.collections.ReflectionAddOpensDemo ;;
treeify)
mvn -B test -Dtest=TreeifyTriggerTest ;;
cme)
mvn -B test -Dtest=ConcurrentModificationTest ;;
eqhash)
mvn -B test -Dtest=EqualsHashCodeContractTest ;;
bucket)
mvn -B test -Dtest=BucketLayoutTest ;;
*)
echo "unknown scenario: $scenario" >&2; exit 1 ;;
esac