Java / Java 21 Collection Framework features Interview questions
Why doesn't HashMap implement SequencedMap?
The same reasoning applies to maps as to sets: SequencedMap promises a defined encounter order for its entries, and HashMap's iteration order depends on hash bucket layout rather than any guaranteed sequence.
Adding firstEntry()/lastEntry() to HashMap would technically compile, but the "first" entry could change between JVM restarts, after rehashing on resize, or even between different runs of the exact same program - none of that is useful or safe to depend on.
Instead, the JDK retrofits SequencedMap onto LinkedHashMap, which tracks insertion (or access) order internally via a linked list threaded through its entries, and onto TreeMap, which maintains a sorted order via its red-black tree structure - both have a real order to expose.
More Related questions...