NavigableMap is an extension of SortedMap which provides convenient navigation method like lowerKey, floorKey, ceilingKey and higherKey, and along with these popular navigation method it also provide ways to create a Sub Map from existing Map in Java e.g. headMap whose keys are less than specified key, tailMap whose keys are greater than specified key and a subMap which is strictly contains keys which falls between toKey and fromKey.
An example class that implements NavigableMap is TreeMap.
Methods of NavigableMap:
- lowerKey(Object key) : Returns the greatest key strictly less than the given key, or if there is no such key.
- floorKey(Object key) : Returns the greatest key less than or equal to the given key, or if there is no such key.
- ceilingKey(Object key) : Returns the least key greater than or equal to the given key, or if there is no such key.
- higherKey(Object key) : Returns the least key strictly greater than the given key, or if there is no such key.
- descendingMap() : Returns a reverse order view of the mappings contained in this map.
- headMap(object toKey, boolean inclusive) : Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.
- subMap(object fromKey, boolean fromInclusive, object toKey, boolean toInclusive) : Returns a view of the portion of this map whose keys range from fromKey to toKey.
- tailMap(object fromKey, boolean inclusive) : Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.
// Java program to demonstrate NavigableMap import java.util.NavigableMap; import java.util.TreeMap; public class Example { public static void main(String[] args) { NavigableMap<String, Integer> nm = new TreeMap<String, Integer>(); nm.put("C", 888); nm.put("Y", 999); nm.put("A", 444); nm.put("T", 555); nm.put("B", 666); nm.put("A", 555); System.out.printf("Descending Set : %s%n", nm.descendingKeySet()); System.out.printf("Floor Entry : %s%n", nm.floorEntry("L")); System.out.printf("First Entry : %s%n", nm.firstEntry()); System.out.printf("Last Key : %s%n", nm.lastKey()); System.out.printf("First Key : %s%n", nm.firstKey()); System.out.printf("Original Map : %s%n", nm); System.out.printf("Reverse Map : %s%n", nm.descendingMap()); } } |
Output:
Descending Set : [Y, T, C, B, A]
Floor Entry : C=888
First Entry : A=555
Last Key : Y
First Key : A
Original Map : {A=555, B=666, C=888, T=555, Y=999}
Reverse Map : {Y=999, T=555, C=888, B=666, A=555}

This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- NavigableMap put() Method in Java
- NavigableMap headMap() in Java
- NavigableMap higherKey() Method in Java
- NavigableMap lastEntry() method in Java
- NavigableMap ceilingEntry() method in Java
- NavigableMap firstEntry() method in Java
- NavigableMap ceilingKey() method in Java
- NavigableMap pollLastEntry() method in Java
- NavigableMap clear() Method in Java
- NavigableMap isEmpty() Method in Java
- NavigableMap size() Method in Java
- NavigableMap higherEntry() method in Java
- NavigableMap pollFirstEntry() method in Java
- NavigableMap floorEntry() method in Java
- NavigableMap floorKey() method in Java



