Java SortedMap Interface: Automatically Sorting Map Entries
Learn about the Java SortedMap
interface, which extends the Map
interface and automatically maintains entries in ascending key order. Discover how it simplifies working with sorted maps and explore the methods that throw exceptions under specific scenarios.
Java - SortedMap Interface
The SortedMap
interface extends the Map
interface and ensures that entries are maintained in an ascending key order. It is useful when you need a map that sorts its entries automatically based on the key.
Several methods of SortedMap
throw exceptions in specific scenarios:
NoSuchElementException
– When no items are in the map.ClassCastException
– When an object is incompatible with the map's elements.NullPointerException
– When trying to use a null object where nulls are not allowed.
SortedMap Interface Methods
The key methods of the SortedMap
interface are summarized below:
Method | Description |
---|---|
Comparator comparator() |
Returns the comparator used for the sorted map. If natural ordering is used, it returns null . |
Object firstKey() |
Returns the first key in the map. |
SortedMap headMap(Object end) |
Returns a view of the portion of the map whose keys are strictly less than end . |
Object lastKey() |
Returns the last key in the map. |
SortedMap subMap(Object start, Object end) |
Returns a view of the portion of the map whose keys are greater than or equal to start and strictly less than end . |
SortedMap tailMap(Object start) |
Returns a view of the portion of the map whose keys are greater than or equal to start . |
Creating a SortedMap
The TreeMap
class implements the SortedMap
interface. To create an instance of SortedMap
, we can use the TreeMap
constructor.
// Create a sorted map
SortedMap<String, Double> map = new TreeMap<>();
This creates a sorted map where the keys are of type String
and the values are of type Double
, sorted in alphanumeric order.
Adding Values to a SortedMap
To add a key-value pair to a SortedMap
, use the put()
method. Each time a new pair is added, the map is re-sorted based on the keys.
public V put(K key, V value)
If the key already exists, the value is updated, and the previous value is returned. Otherwise, null
is returned.
// Put elements to the map
map.put("Zara", 3434.34);
map.put("Mahnaz", 123.22);
map.put("Ayan", 1378.00);
map.put("Daisy", 99.22);
map.put("Qadir", -19.08);
Getting Values from a SortedMap
To retrieve the value associated with a key, use the get()
method.
public V get(Object key)
If the key exists, the associated value is returned; otherwise, null
is returned.
// Get the value associated with a key
Double value = map.get("Qadir");
System.out.print("Qadir: " + value);
Qadir: -19.08
Updating a Value in a SortedMap
You can update a value by calling put()
again with the same key. The map will re-sort itself based on the updated entries.
// Update a value in the map
map.put("Zara", 1378.00);
Removing a Value from a SortedMap
To remove a key-value pair, use the remove()
method.
public V remove(Object key)
The method returns the removed value, or null
if the key doesn't exist.
// Remove a key-value pair from the map
Double value = map.remove("Qadir");
System.out.print("Qadir removed with value: " + value);
Qadir removed with value: -19.08
Iterating over a SortedMap
The entrySet()
method returns all the entries in the form of a Set
, which can be iterated using an Iterator
.
public Set<Map.Entry<K,V>> entrySet()
// Iterate through the map
Set<Map.Entry<String, Double>> set = map.entrySet();
Iterator<Map.Entry<String, Double>> i = set.iterator();
while(i.hasNext()) {
Map.Entry<String, Double> entry = i.next();
System.out.println(entry.getKey() + ": " + entry.getValue());
}
Ayan: 1378.0
Daisy: 99.22
Mahnaz: 123.22
Qadir: -19.08
Zara: 3434.34
Advantages of SortedMap
- Maintains the map sorted by keys in ascending order.
- Efficient for searching in large, read-only datasets.
- Allows custom sorting with a comparator.
Disadvantages of SortedMap
- Performance overhead due to frequent re-sorting when entries are added or updated.
- Keys must implement the
Comparable
interface to be sorted.