TreeMap NavigableMap Methods
Fast recall
Quick revision
Core answer
NavigableMap adds nearest-key operations such as floorKey, ceilingKey, lowerKey, and higherKey, plus bounded views such as headMap, tailMap, and subMap. TreeMap finds boundary keys in O(log n); creating a range view is efficient, but reading k entries from that view still takes O(k) after reaching the start. The views are backed by the map and enforce their bounds.Understanding of TreeMap's sorted capabilities beyond basic put/get operations and knowledge of NavigableMap interface methods
Using TreeMap only for sorted iteration without realizing it provides powerful range query operations that would be expensive with other Maps
Provide a concrete real-world example like finding events in a time range or stock prices closest to a target value
Interview-ready guide
Interview answer
NavigableMap adds operations that use sorted keys. Floor and ceiling find the nearest key at or below or at or above a target, while lower and higher are strict. HeadMap, tailMap, and subMap create bounded views backed by the original map, with overloads for inclusive or exclusive endpoints.
TreeMap locates a boundary in logarithmic time. A range view is cheap to create, but iterating k mappings still takes O(k) after locating the start. Because the view is live, changes affect the original map, and the view rejects keys outside its bounds.
TreeMap NavigableMap Methods
Direct Answer
NavigableMap adds nearest-key operations such as floorKey, ceilingKey, lowerKey, and higherKey, plus bounded views such as headMap, tailMap, and subMap. TreeMap finds boundary keys in O(log n); creating a range view is efficient, but reading k entries from that view still takes O(k) after reaching the start. The views are backed by the map and enforce their bounds.
Code Example
Store events keyed by LocalDateTime, then use the navigation methods to find the next event, the previous event, and ranges of events.
import java.time.LocalDateTime;
import java.util.TreeMap;
record Event(String name, String location) {}
var schedule = new TreeMap<LocalDateTime, Event>();
schedule.put(LocalDateTime.of(2024, 1, 15, 9, 0), new Event("Standup", "Room A"));
schedule.put(LocalDateTime.of(2024, 1, 15, 14, 0), new Event("Review", "Room B"));
schedule.put(LocalDateTime.of(2024, 1, 15, 16, 30), new Event("Planning", "Room C"));
var now = LocalDateTime.of(2024, 1, 15, 15, 0);
var nextEventTime = schedule.ceilingKey(now); // 16:30 Planning
var lastEventTime = schedule.floorKey(now); // 14:00 Review
var morning = schedule.headMap(LocalDateTime.of(2024, 1, 15, 12, 0)); // before noon
var workHours = schedule.subMap(
LocalDateTime.of(2024, 1, 15, 9, 0),
LocalDateTime.of(2024, 1, 15, 17, 0)); // 9am to 5pm range
Each call is O(log n) — finding the next event or a time window never scans the whole schedule.
Common Mistakes & Pitfalls
- Iterating to find the nearest key: looping to find the closest key is
O(n);floorKey()orceilingKey()does it inO(log n)and is the whole reason to useTreeMap. - Confusing floor/ceiling with higher/lower:
floorKeyis less-than-or-equal,lowerKeyis strictly-less-than — mixing them gives off-by-one range boundaries. - Modifying range views wrongly:
subMap()is a live view; putting an out-of-range key into it throwsIllegalArgumentException, which surprises freshers expecting a copy. - Forgetting descending views:
descendingMap()anddescendingKeySet()give reverse iteration with no manual sorting.
Check your understanding
Answer these without looking back at the article.
- What does TreeMap.floorKey() return?
- How do you get all keys in a range from TreeMap?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.