Why this matters
LinkedHashMapgives you a working LRU cache by overriding one method, which is a genuinely useful piece of knowledge.TreeMap's navigation methods solve "find the nearest value at or below this" problems that a hash map cannot express at all.- Interviews frequently ask you to implement an LRU cache, and the one-method version is a strong answer when paired with the manual one.
Bucket index comes from the key's hash, not its insertion order — which is why HashMap iteration has no predictable sequence.
LinkedHashMap
It keeps everything HashMap does and adds a doubly linked list across entries.
Iteration follows insertion order, and re-putting an existing key leaves its position alone. The cost is two extra references per entry and slightly slower insertion.
Access order
The three-argument constructor switches from insertion order to access order, where reading an entry moves it to the end.
The head of the list is now the least recently used entry, which is exactly what a cache needs to evict.
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 6 sections
- An LRU cache in one override
- TreeMap
- Navigation and range queries
- Choosing between the three
- EnumMap, briefly
- Common misreadings
Test yourself
Answer these before moving on — recall is what makes it stick.