NavigableMap in Java | Methods, Example

NavigableMap in Java is an interface that is a subinterface of SortedMap interface. It extends SortedMap interface to handle the retrieval of entries based on the closet match to a given key or keys.

NavigableMap interface was added by Java 1.6 version. It is present in java.util.NavigableMap package. NavigableMap interface is recently added in Java Collections Framework.

TreeMap class is a widely used class that implements NavigableMap interface in Java. NavigableMap interface provides several methods to make map navigation easy.

In simple words, we can navigate the Map easily with NavigationMap interface. We can retrieve the nearest value matching with the specified key, all the values less than the specified key, all values greater than the given key, and so on.

Hierarchy of NavigableMap interface in Java


NavigableMap interface extends SortedMap interface and Map interface. Java ConcurrentNavigableMap interface is the subinterface of NavigableMap interface.

The hierarchy diagram of NavigableMap interface is shown below in the figure.

Java NavigableMap hierarchy diagram

NavigableMap Interface declaration


NavigableMap is a generic interface that is declared as below:

public interface NavigableMap<K,V>
      extends SortedMap<K,V>

[adinserter block=”5″]
Here, K represents the type of keys and V represents the type of values corresponding to keys.

NavigableMap Methods in Java


In addition to the methods that it inherits from the SortedMap interface, NavigableMap interface in Java also adds many new methods. They are as follows:

1. Map.Entry<K,V> ceilingEntry(K obj): This method searches the map for the smallest (least) key k such that key k is greater than or equal to the given key obj (k >= obj).

If such key is found, its entry is returned. If not found such key, the null object will be returned.

2. K ceilingKey(K obj): This method searches the map for the smallest key k such that key k is greater than or equal to the given key obj (k >= obj).

If such key is found, it is returned. If not found such key, the null object will be returned.

3. NavigableSet<K> descendingKeySet(): This method returns a NavigableSet view that contains keys in the invoking map in reverse order. In simple words, it returns a reverse set view of the keys.

4. NavigableMap<K,V> descendingMap(): It returns NavigableMap that is a reverse of invoking map.

5. Map.Entry<K,V> firstEntry(): It returns the first key-value pair associated with the least key in the map, or null if the map is empty.


6. Map.Entry<K,V> floorEntry(K obj): This method searches the largest key k such that the key k is less than or equal to specified key obj (k <= obj).

If such a key is found in the map, its entry is returned. Otherwise, null object is returned.

7. K floorKey(K obj): This method searches the largest key k such that the key k is less than or equal to specified key obj (k <= obj).

If such a key is found in the map, it is returned. Otherwise, the null object is returned.

8. NavigableMap<K,V> headMap(K toKey, boolean inclusive): This method returns a portion of NavigableMap that includes all entries from the invoking map whose keys are less than toKey. If inclusive is true, an element equal to toKey is included.

9. Map.Entry<K,V> higherEntry(K obj): It searches a set for the largest key k such that k > obj. If such a key is found in the map, its key-value pair is returned. Otherwise, the null object is returned.

10. K higherKey(K obj): It searches a set for the largest key k such that k > obj. If a such key is found in the map, it is returned. Otherwise, null object is returned.


11. Map.Entry<K,V> lastEntry(): This method returns the last entry associated with the largest key in the map, or null if the map is empty.

12. Map.Entry<K,V> lowerEntry(K obj): This method searches the set for largest key k such that k < obj. If the key is found in the map, its entry is returned. Otherwise, null object is returned.

13. K lowerKey(K obj): This method searches the set for largest key k such that k < obj. If the key is found in the map, it is returned. Otherwise, null object is returned.

14. NavigableSet<K> navigableKeySet(): It returns a NavigableSet view of the keys contained in the invoking map.


Hope that this tutorial has covered all important points related to NavigableMap interface in Java and its methods. I hope that you will have understood this.
Thanks for reading!!!

⇐ Prev Next ⇒