xxxxxxxxxx
public interface NavigableMap<K,V> extends SortedMap<K,V>
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:
xxxxxxxxxx
public interface NavigableMap<K,V> extends SortedMap<K,V>
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
xxxxxxxxxx
import java.util.NavigableMap;
import java.util.TreeMap;
public class NavigableMapAddElement {
public static void main(String args[]) {
NavigableMap<Integer, String> countries = new TreeMap<>();
// Adding key-value into the map using put() method
countries.put(91, "India");
countries.put(81, "Japan");
countries.put(49, "Germany");
countries.put(65, "Singapore");
countries.put(31, "Netherlands");
System.out.println("Map elements: " + countries);
NavigableMap<Integer, String> otherCountries = new TreeMap<>();
otherCountries.put(973, "Bahrain");
otherCountries.put(358, "Finland");
otherCountries.put(263, "Zimbabwe");
// Copies all of the mappings from the specified map to this map
countries.putAll(otherCountries);
System.out.println("\nNavigableMap Elements after adding new map:");
System.out.println(countries);
}
}
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
xxxxxxxxxx
import java.util.NavigableMap;
import java.util.TreeMap;
public class NavigableMapAccessElement {
public static void main(String args[]) {
NavigableMap<Integer, String> countries = new TreeMap<>();
// Adding key-value into the map using put() method
countries.put(91, "India");
countries.put(81, "Japan");
countries.put(49, "Germany");
countries.put(65, "Singapore");
countries.put(31, "Netherlands");
// Returns the value to which the specified key is mapped
System.out.println("Mapped Value: " + countries.get(49));
// Returns null if this map contains no mapping for the key.
System.out.println("Mapped Value: " + countries.get(100));
// Returns the value to which the specified key is mapped,
// or defaultValue if this map contains no mapping for the key.
System.out.println("Mapped Value: " + countries.getOrDefault(81, "Sorry, no mapping found"));
System.out.println("Mapped Value: " + countries.getOrDefault(200, "Sorry, no mapping found"));
}
}
Output
Mapped Value: Germany
Mapped Value: null
Mapped Value: Japan
Mapped Value: Sorry, no mapping found
Example 3: Remove elements from NavigableMap
xxxxxxxxxx
import java.util.NavigableMap;
import java.util.TreeMap;
public class NavigableMapRemoveElement {
public static void main(String args[]) {
NavigableMap<Integer, String> countries = new TreeMap<>();
// Adding key-value into the map using put() method
countries.put(91, "India");
countries.put(81, "Japan");
countries.put(49, "Germany");
countries.put(65, "Singapore");
countries.put(31, "Netherlands");
// Removes the mapping for a key from this map if it is present
System.out.println("The value associated with the key: " + countries.remove(49));
System.out.println("NavigableMap Elements after Remove:\n" + countries);
// Removes all of the mappings from this map
countries.clear();
// Printing map elements after calling clear() method
System.out.println("NavigableMap Elements: " + countries);
}
}
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
xxxxxxxxxx
import java.util.NavigableMap;
import java.util.TreeMap;
public class NavigableMapUpdateElements {
public static void main(String args[]) {
NavigableMap<Integer, String> countries = new TreeMap<>();
// Adding key-value into the map
countries.put(91, "India");
countries.put(81, "Japan");
countries.put(49, "Germany");
countries.put(65, "Singapore");
countries.put(31, "Netherlands");
// Updating value using put() method
countries.put(81, "Japan-JP");
System.out.println("NavigableMap Elements after updating using put() method:\n" + countries);
// Updating value using replace() method
countries.replace(65, "Singapore-SG");
System.out.println("\nNavigableMap Elements after updating using replace() method:\n" + countries);
}
}
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
xxxxxxxxxx
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
public class NavigableMapTraversing {
public static void main(String[] args) {
NavigableMap<Integer, String> countries = new TreeMap<>();
// Adding key-value into the map
countries.put(91, "India");
countries.put(81, "Japan");
countries.put(49, "Germany");
countries.put(65, "Singapore");
countries.put(31, "Netherlands");
// Traversing the map using java.util.Iterator
System.out.println("Traversing the map using java.util.Iterator");
Iterator<NavigableMap.Entry<Integer, String>> itr = countries.entrySet().iterator();
while (itr.hasNext()) {
NavigableMap.Entry<Integer, String> entry = itr.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
// Traversing the map using for-each loop
System.out.println("\nTraversing the map using for-each loop");
for (Map.Entry<Integer, String> element : countries.entrySet()) {
System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue());
}
// Traversing a map using Map.forEach method and using lambda expression.
System.out.println("\nTraversing a map using Map.forEach method and using lambda expression");
countries.forEach((k, v) -> System.out.println("Key = " + k + ", Value = " + v));
}
}
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
xxxxxxxxxx
import java.util.NavigableMap;
import java.util.TreeMap;
public class NavigableMapUtilityMethods {
public static void main(String args[]) {
NavigableMap<Integer, String> countries = new TreeMap<>();
// Adding key-value into the map using put() method
countries.put(91, "India");
countries.put(81, "Japan");
countries.put(49, "Germany");
countries.put(65, "Singapore");
countries.put(31, "Netherlands");
// Printing map elements
System.out.println("NavigableMap Elements : " + countries);
System.out.println("Size of map : " + countries.size());
// Returns true if this map contains no key-value mappings else false
System.out.println("Is map is empty : " + countries.isEmpty());
// Returns a reverse order navigable set view of the keys in this map
System.out.printf("Descending Set : %s%n", countries.descendingKeySet());
// Returns an entry with the least key,or null if this map is empty
System.out.printf("First Entry : %s%n", countries.firstEntry());
// Returns the last (highest) key currently in this map
System.out.printf("Last Key : %s%n", countries.lastKey());
// Returns the first (lowest) key currently in this map
System.out.printf("First Key : %s%n", countries.firstKey());
// Returns a reverse order view of this map
System.out.printf("Reverse Map : %s%n", countries.descendingMap());
}
}
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
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com