A CLI That Shows EnumMap's Internal State: Ordinal Index, NULL Sentinel, and Eager Allocation
EnumMap's ordinal-indexed storage explains its O(1) lookup without hash collision handling and its inability to support arbitrary keys. This CLI makes the design concrete, so developers understand when to choose EnumMap over HashMap and why null handling behaves the way it does.
Java's EnumMap stores values in a fixed Object[] whose index is the key enum's ordinal. There is no hashing, no resizing, and the array is allocated eagerly at construction. A new CLI demo makes this visible by reflecting into the private fields keyUniverse, vals, size, and the NULL sentinel, then printing the full slot-by-slot state after each operation.
The demo, generated with the help of Trae, shows that put with a null value stores a private sentinel object rather than null, so containsKey stays true while get returns null. It also demonstrates that remove and get return null for both missing keys and null-mapped keys, leaving size as the only way to tell them apart. clear simply fills the entire vals array with null and leaves the array length unchanged.
The tool requires JDK 16+ due to record and text blocks, and JDK 9+ module system restrictions mean it must run with --add-opens=java.base/java.util=ALL-UNNAMED. It works with five built-in enums (Planet, Vowel, Digit, Month, Direction) and accepts piped commands for reproducible output.
The NULL sentinel is a minimal way to distinguish 'no mapping' from 'mapped to null' without boxing, but it forces callers to check containsKey rather than relying on get.
Reflection-based demos like this turn JDK internals into a visual learning tool, but the module system restrictions (--add-opens) show why this approach isn't for production code.
EnumMap's eager allocation trades memory for speed: it always allocates an array as large as the enum's constant count, even if few keys are used.
The CLI's row-per-index output is a better fit than a GUI for displaying array structure because it preserves the ordering and alignment.
The sentinel distinguishes null values from missing keys.
Yes, that's right.