Criteria | NavigableMap | SortedMap |
---|---|---|
Interface Hierarchy | NavigableMap extends SortedMap. | SortedMap is a subinterface of Map. |
Ordering | Guarantees key ordering, like SortedMap. | Guarantees key ordering. |
Primary Methods | Includes methods like lowerEntry(), floorEntry(), ceilingEntry(), higherEntry(), pollFirstEntry(), and pollLastEntry() for navigational operations. | Limited to methods like firstKey(), lastKey(), subMap(), headMap(), and tailMap(). |
View Operations | Supports both ascending and descending views of the map using methods like descendingMap(). | Does not have descending view capabilities. |
Use-Case | When you need enhanced navigational operations in addition to sorted operations. | When you simply need a map with sorted keys. |
Implementations in Java Collections Framework | TreeMap is a notable implementation. | TreeMap implements both SortedMap and NavigableMap, but other custom implementations can limit themselves to just SortedMap methods. |
Thread Safety | Standard implementations like TreeMap are not thread-safe. Synchronization must be handled externally if concurrent modifications are expected. | Same as NavigableMap. |
Example: Difference Between NavigableMap and SortedMap in Java
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;
public class NavigableMapVsSortedMapDemo {
public static void main(String[] args) {
// Using a SortedMap
SortedMap<String, Integer> sortedMap = new TreeMap<>();
sortedMap.put("A", 1);
sortedMap.put("B", 2);
sortedMap.put("C", 3);
// Using a NavigableMap
NavigableMap<String, Integer> navigableMap = new TreeMap<>();
navigableMap.put("A", 1);
navigableMap.put("B", 2);
navigableMap.put("C", 3);
System.out.println("SortedMap First Key: " + sortedMap.firstKey());
System.out.println("NavigableMap First Key: " + navigableMap.firstKey());
// Demonstrating a feature of NavigableMap
System.out.println("NavigableMap Lower Entry for 'B': " + navigableMap.lowerEntry("B"));
}
}
Output:
SortedMap First Key: A NavigableMap First Key: A NavigableMap Lower Entry for 'B': A=1
Explanation:
1. SortedMap: It is an interface in Java that extends Map. It ensures that the entries are maintained in ascending key order. The SortedMap provides methods like firstKey() and lastKey() to retrieve the first and last key.
2. NavigableMap: It is a sub-interface of SortedMap and provides several methods that allow for more flexible navigation, like lowerEntry(), floorEntry(), ceilingEntry(), and higherEntry().
3. In the provided example:
– Both SortedMap and NavigableMap have the same ordering of keys and both can retrieve the first key using the firstKey() method.
– The NavigableMap demonstrates its unique capability with the lowerEntry() method which returns the key-value mapping associated with the greatest key strictly less than the given key.
4. When to use:
– Use SortedMap when you just need to ensure that the keys are in a sorted order and want to retrieve basic boundaries like the first and last key.
– Use NavigableMap when you need advanced navigation methods to navigate through the map based on proximity to a given key.
In summary, while SortedMap offers basic operations in a sorted order, NavigableMap provides more advanced navigational operations.