When do you reach for TreeMap/NavigableMap instead of HashMap?
Reviewed by Gurusankar M.
Short Answer
When you need sorted keys or range/nearest queries: floorKey, ceilingKey, headMap, tailMap, subMap. TreeMap is O(log n) (red-black tree) vs HashMap's O(1) — you trade speed for ordering and range operations.
On this page
Concept Made Simple
Coffee Chat Question
“When do you reach for TreeMap/NavigableMap instead of HashMap?”
Remember It Faster
Mind Map Answer
- floor/ceiling
- nearest key ≤ / ≥ target
- subMap/headMap/tailMap
- range views
- firstKey/lastKey
- min / max
Learn by Doing
Hands-on Keyboard
NavigableMap<Integer,String> tiers = new TreeMap<>();
tiers.put(0,"Bronze"); tiers.put(1000,"Silver"); tiers.put(5000,"Gold");
// which tier for 2300 points?
System.out.println(tiers.floorEntry(2300).getValue());Silver
- Time:
- O(log n) lookup/range
Think Beyond the Expected
What If?
How would you find all events in a time window efficiently?
Key a TreeMap by timestamp and call subMap(start, end) — it returns a view of just that range in O(log n + k), far better than scanning a HashMap or list and filtering.
Real World
Pricing tiers, rate cards, time-series windows and 'nearest value' lookups (e.g. find the applicable discount band) are natural NavigableMap problems.
Interviewer's Expectation
Keywords they're listening for:
- sorted order
- floor/ceiling/sub-views
- O(log n)
- Comparable/Comparator
- range queries
Common Mistakes
- Scanning + filtering a HashMap for range queries
- Using TreeMap when no ordering is needed (slower)
- Keys not Comparable and no Comparator supplied
Best Practices
- Use NavigableMap for range/nearest queries
- Supply a Comparator for custom key ordering
- Stick with HashMap when you only need point lookups
Follow-up Questions
- What's the cost difference vs HashMap and when does it matter?
- How do you do a range scan with subMap?
- What ordering contract must keys satisfy?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: TreeMap (Java Collections) Interview question: "When do you reach for TreeMap/NavigableMap instead of HashMap?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair