NavigableMap is an interface which is available in java.util package which provides provides the feature to navigate among the map entries
NavigableMap is an interface, we cannot create objects from it. In order to use the functionalities of the NavigableMap interface, we need to use the class TreeMap that implements it.
How to use NavigableMap:
NavigableMap<Key, Value> numbers = new TreeMap<>();
Method Name | Description |
---|---|
headMap(key, booleanValue) | The headMap() method returns all the entries of a navigable map associated with all those keys before the specified key. |
tailMap(key, booleanValue) | The tailMap() method returns all the entries of a navigable map associated with all those keys after the specified key (which is passed as an argument) including the entry associated with the specified key.. |
subMap(k1, bv1, k2, bv2) | The subMap() method returns all the entries associated with keys between k1 and k2 including the entry associated with k1. The bv1 and bv2 are optional parameters. The default value of bv1 is true and the default value of bv2 is false. |
descendingMap() | reverse the order of entries in a map. |
descendingKeyMap() | reverses the order of keys in a map. |
ceilingEntry() | returns an entry with the lowest key among all those entries whose keys are greater than or equal to the specified key. |
ceilingKey() | returns the lowest key among those keys that are greater than or equal to the specified key. |
floorEntry() | returns an entry with the highest key among all those entries whose keys are less than or equal to the specified key. |
floorKey() | returns the highest key among those keys that are less than or equal to the specified key. |
higherEntry() | returns an entry with the lowest key among all those entries whose keys are greater than the specified key. |
higherKey() | returns the lowest key among those keys that are greater than the specified key. |
lowerEntry() | returns an entry with the highest key among all those entries whose keys are less than the specified key. |
lowerKey() | returns the highest key among those keys that are less than the specified key. |
firstEntry() | returns the first entry (the entry with the lowest key) of the map. |
lastEntry() | returns the last entry (the entry with the highest key) of the map. |
pollFirstEntry() | returns and removes the first entry of the map. |
pollLastEntry() | returns and removes the last entry of the map. |
import java.util.NavigableMap;
import java.util.TreeMap;
class Main {
public static void main(String[] args) {
// Creating NavigableMap using TreeMap
NavigableMap<String, Integer> numbers = new TreeMap<>();
// Insert elements to map
numbers.put("Two", 2);
numbers.put("One", 1);
numbers.put("Three", 3);
System.out.println("NavigableMap: " + numbers);
// Access the first entry of the map
System.out.println("First Entry: " + numbers.firstEntry());
// Access the last entry of the map
System.out.println("Last Entry: " + numbers.lastEntry());
// Remove the first entry from the map
System.out.println("Removed First Entry: " + numbers.pollFirstEntry());
// Remove the last entry from the map
System.out.println("Removed Last Entry: " + numbers.pollLastEntry());
}
}