HashMap vs LinkedHashMap vs TreeMap Java | Fresher Interview
Fast recall
Quick revision
Core answer
Map. HashMap has no order and O(1) average ops. LinkedHashMap keeps insertion order (or access order) via a linked list through the entries, still O(1). TreeMap keeps keys in sorted order using a red-black tree, with O(log n) ops. Pick HashMap for speed, LinkedHashMap to preserve order, TreeMap when you need sorted keys or range queries.Whether you know the ordering guarantees and complexity differences, not just that all are maps
Thinking HashMap preserves insertion order, or that TreeMap is O(1)
Mention TreeMap's range methods (floorKey, ceilingKey, subMap) as the real reason to choose it
Interview-ready guide
Interview answer
All three implement the Map interface, so they share the same basic API, but they differ in ordering and performance.
HashMap is the default. It's backed by a bucket array, it gives no ordering guarantee at all, and its get and put are O of one on average. The mistake to avoid is assuming HashMap preserves insertion order; it doesn't, and the iteration order can even change after a resize.
LinkedHashMap extends HashMap and additionally threads a doubly linked list through all the entries, so it remembers the order you inserted keys, and iteration follows that order. It's still O of one. It can also run in access-order mode, which is exactly what you want for building an LRU cache, combined with overriding removeEldestEntry.
TreeMap is different: it's backed by a red-black tree and keeps the keys in sorted order. That sorting costs you, so its operations are O of log n instead of O of one.
But in return you get navigation methods that the others don't have, like floorKey for the largest key less than or equal to a value, ceilingKey for the smallest key greater than or equal, and subMap for a sorted range. So my decision rule is simple. If I just need fast lookups and don't care about order, HashMap.
If I need to preserve insertion order or build an LRU cache, LinkedHashMap. If I need sorted keys or range queries, TreeMap, and I accept the log n cost. One more detail: TreeMap doesn't allow a null key, because it has to compare keys to sort them.
HashMap vs LinkedHashMap vs TreeMap Java | Fresher Interview
Direct Answer
All three implement Map. HashMap has no order and O(1) average ops. LinkedHashMap keeps insertion order (or access order) via a linked list through the entries, still O(1). TreeMap keeps keys in sorted order using a red-black tree, with O(log n) ops. Pick HashMap for speed, LinkedHashMap to preserve order, TreeMap when you need sorted keys or range queries.
Common Mistakes & Pitfalls
Thinking HashMap preserves insertion order, or that TreeMap is O(1)
Check your understanding
Answer these without looking back at the article.
- How do you implement an LRU cache using LinkedHashMap?
- When would you use TreeMap over HashMap?
- Does TreeMap allow null keys?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.