English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about the Java NavigableMap interface and its methods through an example.
The NavigableMap interface of the Java collection framework provides the functionality to navigate between mapping entries.
it is consideredSortedMapa type of
Since NavigableMap is an interface, objects cannot be created from it.
To use the functions of the NavigableMap interface, we need to use the TreeMap class that implements NavigableMap.
In Java, we must import the java.util.NavigableMap package to use NavigableMap. After importing the package, NavigableMap will be created as follows.
// NavigableMap is implemented by the TreeMap class. NavigableMap<Key, Value> numbers = new TreeMap<>();
In the above code, we created a navigable map named numbers of the TreeMap class.
Here,
Key - Used as a unique identifier to associate each element (value) in the mapping
Value - Elements associated with the key in the map
NavigableMap is considered a type of SortedMap. This is because NavigableMap inherits the SortedMap interface.
Therefore, all SortedMap methods can also be used in NavigableMap. To learn how to define these methods in SortedMap, please visitJava SortedMap.
However, certain methods of SortedMap (headMap(), tailMap(), and subMap()) in NavigableMap are defined differently.
Let's see how to define these methods in NavigableMap.
The headMap() method returns all entries associated with keys before the specified key (key) as a parameter.
booleanValue is an optional parameter. The default value is false.
If booleanValue is true, this method returns all entries associated with all those keys before the specified key (key), including those associated with the specified key (key).
The tailMap() method returns all entries associated with keys after the specified key (key) as a parameter, including those associated with the specified key (key).
booleanValue is an optional parameter. The default value is true.
If booleanValue is false, this method will return all entries associated with keys after the specified key (key), excluding those associated with the specified key (key).
The subMap() method returns those associated with k1and k2All entries associated with the key, including those associated with k1related entries.
bv1and bv2is an optional parameter. bv1is set to true, bv2is set to false by default.
if bv1returns those associated with k1and k2All entries associated with the key, excluding those associated with k1The associated entry.
if bv2If the bv is true, then this method returns the entries associated with k1and k2All entries between the keys associated with k1The associated entry.
NavigableMap provides various methods that can be used to locate entries in the mapping.
DescendingMap() - Reverse the order of entries in the map
DescendingKeyMap() - Reverse the order of keys in the map
ceilingEntry() - Return the entry with the minimum key among all entries with keys greater than or equal to the specified key
ceilingKey() - Return the minimum key among the keys greater than or equal to the specified key
floorEntry() - Return the entry with the maximum key among all entries with keys less than or equal to the specified key
floorKey() - Return the maximum key among the keys less than or equal to the specified key
HigherEntry() - Return the entry with the minimum key among all entries with keys greater than the specified key
HigherKey() - Return the minimum key among the keys greater than the specified key
lowerEntry() - Return the entry with the maximum key among all entries with keys less than the specified key
lowerKey() - Return the maximum key among the keys less than the specified key
firstEntry() - Return the first entry of the map (the entry with the minimum key)
lastEntry() - Return the last entry of the map (the entry with the maximum key)
pollFirstEntry() - Return and delete the first entry of the map
pollLastEntry() - Return and delete the last entry of the map
import java.util.NavigableMap; import java.util.TreeMap; class Main { public static void main(String[] args) { //Create a NavigableMap using TreeMap NavigableMap<String, Integer> numbers = new TreeMap<>(); //Insert elements into the map numbers.put("Two", 2); numbers.put("One", 1); numbers.put("Three", 3); System.out.println("NavigableMap: " + numbers); //Access the first entry of the mapping System.out.println("The first item: " + numbers.firstEntry()); //Access the last item of the map System.out.println("The last item: ", + numbers.lastEntry()); //Delete the first item from the map System.out.println("Delete the first item: ", + numbers.pollFirstEntry()); //Delete the last item from the map System.out.println("Delete the last item: ", + numbers.pollLastEntry()); {} {}
Output Result
NavigableMap: {One=1, Three=3, Two=2{} The first item: One=1 The last item: Two=2 Delete the first item: One=1 Delete the last item: Two=2
For more information on TreeMap, please visitJava TreeMap.
Now that we know about the NavigableMap interface, we will use the TreeMap class in the next tutorial to understand its implementation in detail.