Sub topic 2
The ordered map and sorted set interface
This chapter assumes you know how to use maps and sets, can read Big-O notation, and understand binary search. It defines the interface that every ordered collection shares. Later chapters cover the structures that implement it.
What the interface adds
A plain map offers put, get, remove and containsKey. An ordered map keeps its keys in sorted order and adds operations that depend on that order. A sorted set is the same idea with keys only and no values.
The operations fall into four groups:
| Group | Operations | Question answered |
|---|---|---|
| Basic | put, get, remove, contains | Is this exact key present? |
| Extremes | first, last, pollFirst, pollLast | What is the smallest or largest key? |
| Navigation | floor, ceiling, lower, higher | What is the nearest key to this one? |
| Ranges | subMap, headMap, tailMap | Which keys fall between two bounds? |
Iteration also changes. A hash map iterates in no useful order. An ordered map always iterates from the smallest key to the largest.
The four navigation operations
Given a key k, these operations find a neighbor in the sorted key sequence:
floor(k)returns the greatest key less than or equal tok.ceiling(k)returns the least key greater than or equal tok.lower(k)returns the greatest key strictly less thank.higher(k)returns the least key strictly greater thank.
k does not have to be in the collection. This is why ordered maps can turn a table of thresholds into a lookup, as in the example below. Each operation returns nothing (null in Java) when no such key exists. Check for that before you use the result.
import java.util.TreeMap;
public class GradeLookup {
public static void main(String[] args) {
TreeMap<Integer, String> grades = new TreeMap<>();
grades.put(60, "D");
grades.put(70, "C");
grades.put(80, "B");
grades.put(90, "A");
int score = 85;
String grade = grades.floorEntry(score).getValue();
System.out.println(grade); // B
}
}Here floorEntry(85) finds the entry with key 80, because 80 is the greatest threshold not above 85. A score below 60 would make floorEntry return null, so real code needs a default.
When the query key is present, the inclusive and strict versions differ. For key 80, floor(80) and ceiling(80) both return 80. lower(80) returns 70 and higher(80) returns 90.
Views, not copies
Range operations such as subMap(from, to) return a view. The result is not a copy. The lower bound is inclusive and the upper bound is exclusive by default.
import java.util.SortedMap;
import java.util.TreeMap;
public class RangeView {
public static void main(String[] args) {
TreeMap<Integer, String> grades = new TreeMap<>();
grades.put(60, "D");
grades.put(70, "C");
grades.put(80, "B");
grades.put(90, "A");
SortedMap<Integer, String> middle = grades.subMap(70, 90);
System.out.println(middle); // {70=C, 80=B}
}
}Because middle is a view, a later change to grades inside that range shows up in middle. A change to the view writes through to the original map.
Exercise
A TreeMap holds the keys 10, 20 and 30. What do floor(25), ceiling(25), lower(20) and higher(30) return?
Show answer
floor(25) returns 20, because 20 is the greatest key at or below 25. ceiling(25) returns 30, the least key at or above 25. lower(20) returns 10, the greatest key strictly below 20. higher(30) returns null, because no key is greater than 30.