NavigableMap Interface in Java

NavigableMap Interface in Java

The NavigableMap interface in Java is a subinterface of the SortedMap interface that provides methods for navigating through the map and performing efficient range operations. It extends the SortedMap interface to support efficient retrieval and manipulation of key-value pairs based on their order.

Key Features

  • Extends SortedMap Interface: The NavigableMap interface extends the SortedMap interface, which means it inherits all the methods from the SortedMap interface.
  • Navigation Methods: NavigableMap provides methods for navigating through the map, such as lowerKey(K key), higherKey(K key), floorKey(K key), and ceilingKey(K key). These methods allow you to retrieve the keys that are less than, greater than, less than or equal to, or greater than or equal to a given key.
  • Range Operations: NavigableMap also provides methods for performing range operations, such as subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive), headMap(K toKey, boolean inclusive), and tailMap(K fromKey, boolean inclusive). These methods allow you to retrieve a submap of key-value pairs within a specified range.

Methods

The NavigableMap interface includes all the methods from the SortedMap interface, along with additional methods for navigation and range operations. Some of the important methods include:


K lowerKey(K key)
K higherKey(K key)
K floorKey(K key)
K ceilingKey(K key)
NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
NavigableMap<K, V> headMap(K toKey, boolean inclusive)
NavigableMap<K, V> tailMap(K fromKey, boolean inclusive)

Example

Here's an example of using the NavigableMap interface in Java:


import java.util.NavigableMap;
import java.util.TreeMap;

public class NavigableMapExample {
    public static void main(String[] args) {
        NavigableMap>Integer, String< navigableMap = new TreeMap><();

        navigableMap.put(1, "Apple");
        navigableMap.put(3, "Banana");
        navigableMap.put(5, "Orange");
        navigableMap.put(7, "Mango");
        navigableMap.put(9, "Pineapple");

        NavigableMap>Integer, String< subMap = navigableMap.subMap(3, true, 7, false);
        for (Integer key : subMap.keySet()) {
            System.out.println(key + " : " + subMap.get(key));
        }
        
    }
}

Output:

  3 : Banana
  5 : Orange

In this example, we use the TreeMap class, which implements the NavigableMap interface. We create a NavigableMap of Integer keys and String values and add five key-value pairs to it. We then use the subMap method to retrieve a submap of key-value pairs from 3 (inclusive) to 7 (exclusive). The output shows the entries within the specified range, which are 3 and 5 in this case.

Conclusion

The NavigableMap interface in Java extends the SortedMap interface to provide methods for efficient navigation and range operations. It allows you to retrieve keys based on their order and perform various range-related operations. By using the NavigableMap interface, you can efficiently work with sorted key-value pairs in your Java applications.

I hope this explanation helps you understand the NavigableMap interface in Java! Let me know if you have any further questions.

List      Set      Queue      Map     TreeMap     Collection     Comparable Interface     Comparator Interface

Post a Comment

Previous Post Next Post