hashmap-concurrenthashmap: companion code for the HashMap vs ConcurrentHashMap interview guide

Adds JMH put/get throughput benchmarks (synchronizedMap vs ConcurrentHashMap,
swept at 1/2/4/8 threads), a computeIfAbsent recursion trap demo (CHM tolerates
recursion into a different key but throws IllegalStateException on the same
key; plain HashMap throws ConcurrentModificationException even single-threaded),
and a null-handling demo (CHM rejects null keys/values on put() and null keys
on get() too). 7/7 JUnit tests pass; all output/ transcripts captured from
real runs.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FhzLY5p6okFva3qsnsRyvM
This commit is contained in:
2026-09-30 09:29:06 +00:00
co-authored by Claude Sonnet 5
parent af615cc17c
commit 4a74153d87
16 changed files with 790 additions and 0 deletions
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
# Regenerates every file in ../output/ from a real run against $JDK25_HOME.
# Usage: JDK25_HOME=/path/to/jdk-25 ./run-all.sh
set -euo pipefail
if [ -z "${JDK25_HOME:-}" ]; then
echo "Set JDK25_HOME to a JDK 25 install first." >&2
exit 1
fi
cd "$(dirname "$0")/.."
OUT="output"
mkdir -p "$OUT"
mvn -q package
CP="target/classes"
run() {
local cls="$1" out="$2"
{ echo "\$ java $cls"; echo; "$JDK25_HOME/bin/java" -cp "$CP" "com.ankurm.hashmapconcurrenthashmap.$cls"; } 2>&1 \
| grep -v "^Picked up JAVA_TOOL_OPTIONS" > "$OUT/$out"
}
run ComputeIfAbsentRecursionDemo 01-computeifabsent-recursion.txt
run NullHandlingDemo 02-null-handling.txt
for t in 1 2 4 8; do
{ echo "\$ java -jar benchmarks.jar PutGetBenchmark -t $t"; echo; \
"$JDK25_HOME/bin/java" -jar target/benchmarks.jar PutGetBenchmark -t "$t"; } 2>&1 \
| grep -v "^Picked up JAVA_TOOL_OPTIONS" > "$OUT/0$((t == 1 ? 3 : t == 2 ? 4 : t == 4 ? 5 : 6))-put-get-benchmark-t$t.txt"
done
cp target/surefire-reports/com.ankurm.hashmapconcurrenthashmap.MapClaimsTest.txt "$OUT/07-correctness-tests.txt"
echo "Done. See $OUT/"