Mastering Java NavigableMap: Features, Methods, and TreeMap Implementation
Java NavigableMap Interface
This tutorial explains the Java NavigableMap interface, its core methods, and how to use it with TreeMap.
The NavigableMap interface of the Java collections framework provides navigation capabilities across map entries. It is a specialized SortedMap, extending its functionality with methods that allow efficient bidirectional traversal.
Class that Implements NavigableMap
Because NavigableMap is an interface, you cannot instantiate it directly. The primary implementation is the TreeMap class, which offers a self‑balancing binary search tree structure that maintains keys in sorted order.

How to Use NavigableMap
First, import the interface:
import java.util.NavigableMap;
import java.util.TreeMap;
Then create an instance:
// NavigableMap implementation by TreeMap class
NavigableMap<Key, Value> numbers = new TreeMap<>();
In the code above, numbers is a TreeMap that stores key–value pairs. Key is the unique identifier, and Value is the associated data.
Core Methods of NavigableMap
All SortedMap operations are available, but several are overridden to provide tighter control. Key methods include:
headMap(key, booleanInclusive)
Returns a view of the portion of the map whose keys are strictly less than key when booleanInclusive is false (default). If booleanInclusive is true, the returned map includes the entry for key.
tailMap(key, booleanInclusive)
Returns a view of the portion of the map whose keys are greater than or equal to key when booleanInclusive is true (default). Passing false excludes the key entry.
subMap(fromKey, fromInclusive, toKey, toInclusive)
Provides a view between fromKey and toKey. The defaults are fromInclusive = true and toInclusive = false. Adjusting these flags controls whether the boundary keys are part of the resulting map.
Additional Navigation Methods
- descendingMap() – reverses entry order.
- descendingKeySet() – reversed key view.
- ceilingEntry(key) – lowest entry with key >= given key.
- ceilingKey(key) – lowest key >= given key.
- floorEntry(key) – highest entry with key <= given key.
- floorKey(key) – highest key <= given key.
- higherEntry(key) – lowest entry with key > given key.
- higherKey(key) – lowest key > given key.
- lowerEntry(key) – highest entry with key < given key.
- lowerKey(key) – highest key < given key.
- firstEntry() – entry with the smallest key.
- lastEntry() – entry with the largest key.
- pollFirstEntry() – retrieves and removes the first entry.
- pollLastEntry() – retrieves and removes the last entry.
For the official specification, see the Java NavigableMap documentation.
TreeMap Implementation Example
import java.util.NavigableMap;
import java.util.TreeMap;
class Main {
public static void main(String[] args) {
// Creating NavigableMap using TreeMap
NavigableMap<String, Integer> numbers = new TreeMap<>();
// Insert elements into the map
numbers.put("Two", 2);
numbers.put("One", 1);
numbers.put("Three", 3);
System.out.println("NavigableMap: " + numbers);
// Access the first entry of the map
System.out.println("First Entry: " + numbers.firstEntry());
// Access the last entry of the map
System.out.println("Last Entry: " + numbers.lastEntry());
// Remove the first entry from the map
System.out.println("Removed First Entry: " + numbers.pollFirstEntry());
// Remove the last entry from the map
System.out.println("Removed Last Entry: " + numbers.pollLastEntry());
}
}
Output
NavigableMap: {One=1, Three=3, Two=2}
First Entry: One=1
Last Entry: Two=2
Removed First Entry: One=1
Removed Last Entry: Two=2
Learn more about TreeMap in the Java TreeMap documentation.
With a solid grasp of the NavigableMap interface and its TreeMap implementation, you can build efficient, sorted data structures for your Java applications.
Java
- Mastering Java Interfaces: Concepts, Implementation, and Best Practices
- Java Collections Framework: Core Interfaces, Implementations, and Practical Usage
- Java Collection Interface: Core Concepts & Essential Methods
- Mastering Java’s Queue Interface: Methods, Implementations, and Practical Use
- Mastering Java's Deque Interface: Features, Methods, and Practical Examples
- Java Map Interface – Comprehensive Guide to Map, Its Implementations, and Key Methods
- Java SortedMap Interface: Overview, Methods, and TreeMap Implementation
- Mastering Java’s ConcurrentMap: Thread‑Safe Maps Explained
- Mastering Java’s Set Interface: Concepts, Methods, and Practical Examples
- Mastering Java SortedSet: A Practical Guide to TreeSet and Its Methods