sortedMap interface in java
The SortedMap interface in Java is a subtype of the Map interface that extends it to maintain its keys in ascending order. It is a part of the Java Collections Framework and represents a collection of key-value pairs where keys are sorted based on their natural ordering or a custom comparator provided by the user.
Table of Contents
Features of the SortedMap Interface:
- 1. Sorted Ordering:
- SortedMap maintains its keys in sorted (ascending) order.
- The order can be based on the natural ordering of the keys or a custom comparator provided by the user.
- 2. Key-Based Operations:
- Like other map implementations, SortedMap provides key-based operations such as put, get, remove, and containsKey.
- 3. Submap Views:
- SortedMap allows the creation of submap views, which represent a range of keys within the map.
- Submap views can be obtained using the subMap, headMap, and tailMap methods.
- 4. Comparable Keys:
- If the keys stored in the SortedMap are comparable (i.e., they implement the Comparable interface), then the natural ordering of the keys is used for sorting.
- 5. Custom Comparator:
- Alternatively, if the keys are not comparable or if a custom ordering is desired, a Comparator can be provided at the time of SortedMap creation to specify the sorting order.
- 6. NavigableMap Operations:
- SortedMap extends the NavigableMap interface, which provides additional navigation and search operations such as ceilingKey, floorKey, higherKey, lowerKey, etc.
Common Methods of the SortedMap Interface:
- comparator(): Returns the comparator used to order the keys in this map, or null if the natural ordering is used.
- firstKey(): Returns the first (lowest) key currently in the map.
- lastKey(): Returns the last (highest) key currently in the map.
- subMap(K fromKey, K toKey): Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
- headMap(K toKey): Returns a view of the portion of this map whose keys are strictly less than toKey.
- tailMap(K fromKey): Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
Example Usage of SortedMap Interface
```java
import java.util.SortedMap;
import java.util.TreeMap;
public class SortedMapExample {
public static void main(String[] args) {
// Create a SortedMap (TreeMap in this case)
SortedMap<String, Integer> sortedMap = new TreeMap<>();
// Add key-value pairs to the SortedMap
sortedMap.put("Apple", 10);
sortedMap.put("Banana", 20);
sortedMap.put("Cherry", 15);
// Print the first and last keys
System.out.println("First Key: " + sortedMap.firstKey());
System.out.println("Last Key: " + sortedMap.lastKey());
// Print all key-value pairs in the SortedMap
System.out.println("Key-Value Pairs:");
for (String key : sortedMap.keySet()) {
System.out.println(key + ": " + sortedMap.get(key));
}
}
}
```
In this example:
- We create a SortedMap (TreeMap) and add some key-value pairs to it.
- We retrieve and print the first and last keys in the SortedMap using the firstKey() and lastKey() methods.
- We iterate over all key-value pairs in the SortedMap and print them out.
The SortedMap interface is commonly used in scenarios where keys need to be maintained in sorted order, such as building sorted indexes or storing sorted data. It provides efficient access to keys and supports various navigation and search operations.