NavigableMap Interface in Java
In the previous blog, we learned about the SortedMap interface. If you want to learn more about it, visit, visit SortedMap Interface in Java. In this blog, we will go through another interface called the NavigableMap interface.
NavigableMap Interface
NavigableMap interface can be accessed and traversed in ascending or descending order. It is an extension of SortedMap which means it supports the methods of SortedMap along with some new methods. NavigableMap is present in java.util package.
Syntax of NavigableMap
Creating a NavigableMap
As we have already seen in the previous blogs that both SortedMap and NavigableMap are interfaces that cannot be used as a class, so we need a class TreeMap that implements them.
Methods of NavigableMap
- ceilingEntry()
- ceilingKey()
- descendingKeySet()
- descendingMap()
- firstEntry()
- floorEntry()
- higherEntry()
- higherKey()
- lastEntry()
- lowerEntry()
- lowerKey()
- pollFirstEntry()
- pollLastEntry()
ceilingEntry() method returns the key value mapping associated with the lowest key which is greater than or equal to the given key or returns null if there is no such key.
e.g.
Output
ceilingKey() returns the lowest key that is greater than or equal to the specified key, or null if no such key exists.
e.g.
Output
descendingKeySet() method returns the reverse order NavigableSet view for the given keys that are contained in the map.
e.g.
Output
The descendingMap() method returns the map's mapping in reverse order.
e.g.
Output
The firstEntry() method returns the entry of the map with the lowest key, or null if the map is empty.
e.g.
Output
If no such key is discovered, the floorEntry() method delivers the entry with the greatest ky that is less than or equal to the specified key, or it returns null.
e.g.
Output
higherEntry() method returns the entry with the lowest key amongst those keys which are greater than the given key or returns null if there is no such key found.
e.g.
Output
higherKey() method returns the lowest key which is greater than the given key or returns null if there is no such key found.
e.g.
Output
The lastEntry() method returns the map entry with the highest key, or null if the map is empty.
e.g.
Output
lowerEntry() method returns the entry with the highest key that is less than the specified key or returns null if the map is empty.
e.g.
Output
lowerKey() method returns the highest key which is less than the given key or returns null if the is no such key found.
e.g.
Output
pollFirstEntry() returns and deletes the map's first entry, or returns null if the map is empty.
e.g.
Output
pollLastEntry() returns and remove the last entry of the map or returns null if the map is empty
e.g.
Output