Last Updated: 06 March, 2023
The NavigableMap is an interface in the Java collections framework. It was introduced in the JDK 1.6 version and is available in the java.util package.
NaviableMap provides convenient navigation methods like lowerKey, floorKey, ceilingKey, and higherKey along with this popular navigation method. It also provides ways to create a submap from an existing map in Java, e.g., a headmap whose keys are less than the specified key, a tailmap whose keys are greater than the specified key, and a submap that strictly contains keys that fall between toKey and fromKey.
NavigableMap Interface Declaration:
Here, K is the key Object type and V is the value Object type.
NavigableMap extends the SortedMap interface to handle the retrieval of entries based on the closest match to a given key or keys.
NavigableMap inherits all the methods of the Map interface as well as the SortedMap interface, but it also has many other methods. They are as follows:
The TreeMap class is a widely used class that implements the NavigableMap interface in Java. Let's see the examples given below:
Example 1: Add elements into NavigableMap
Output
Map elements: {31=Netherlands, 49=Germany, 65=Singapore, 81=Japan, 91=India}
NavigableMap Elements after adding new map:
{31=Netherlands, 49=Germany, 65=Singapore, 81=Japan, 91=India, 263=Zimbabwe, 358=Finland, 973=Bahrain}
Example 2: Access elements from NavigableMap
Output
Mapped Value: Germany
Mapped Value: null
Mapped Value: Japan
Mapped Value: Sorry, no mapping found
Example 3: Remove elements from NavigableMap
Output
The value associated with the key: Germany
NavigableMap Elements after Remove:
{31=Netherlands, 65=Singapore, 81=Japan, 91=India}
NavigableMap Elements: {}
Example 4: Update elements into NavigableMap
Output
NavigableMap Elements after updating using put() method:
{31=Netherlands, 49=Germany, 65=Singapore, 81=Japan-JP, 91=India}
NavigableMap Elements after updating using replace() method:
{31=Netherlands, 49=Germany, 65=Singapore-SG, 81=Japan-JP, 91=India}
Example 5: Traversing NavigableMap
Output
Traversing the map using java.util.Iterator
Key=31, Value=Netherlands
Key=49, Value=Germany
Key=65, Value=Singapore
Key=81, Value=Japan
Key=91, Value=India
Traversing the map using for-each loop
Key=31, Value=Netherlands
Key=49, Value=Germany
Key=65, Value=Singapore
Key=81, Value=Japan
Key=91, Value=India
Traversing a map using Map.forEach method and using lambda expression
Key=31, Value=Netherlands
Key=49, Value=Germany
Key=65, Value=Singapore
Key=81, Value=Japan
Key=91, Value=India
Example 6: Utility method of NavigableMap
Output
NavigableMap Elements :{31=Netherlands, 49=Germany, 65=Singapore, 81=Japan, 91=India}
Size of map : 5
Is map is empty : false
Descending Set : [91, 81, 65, 49, 31]
First Entry : 31=Netherlands
Last Key : 91
First Key : 31
Reverse Map :{91=India, 81=Japan, 65=Singapore, 49=Germany, 31=Netherlands}
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/NavigableMap.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com