In Java, lowerKey(key) returns the greatest key strictly less thankey in a TreeMap, or null if there is none. Unlike floorKey(), an exact match is skipped: the result is always the key before the probe.
"Less" follows the map's comparator, so in a reverse-ordered map lowerKey returns a numerically larger key. It is part of NavigableMap (Java 6) and runs in O(log n) time.
An instance method of java.util.TreeMap, added in Java 6 (JDK 1.6) with the NavigableMap interface. A TreeMap keeps its key/value pairs sorted by key in a red-black tree, so lookups and updates take O(log n) time; it is not thread-safe.
Syntax
Pass the probe; the answer is the key just before it.
Walking backwards is a loop of k = map.lowerKey(k) until null; for a full reverse pass, iterating descendingMap() is simpler and avoids a fresh search per step.
Library declarations
Java
// java.util.TreeMap<K, V>
public K lowerKey(K key)
// Declared in NavigableMap<K, V> (Java 6)
Parameters
Parameter
Description
key (K)
The probe value; it does not need to be a key in the map.
Return Value
Return Value
Description
K
The greatest key that compares strictly less than key.
Example 1 — Which Checkpoint Did the Runner Pass Last?
Keys are kilometre markers on a marathon course; the probe is the runner's position.
Java
import java.util.TreeMap;
public class TreeMapLowerKeyCheckpoints {
public static void main(String[] args) {
TreeMap<Integer, String> checkpoints = new TreeMap<>();
checkpoints.put(0, "Start");
checkpoints.put(10, "Bridge");
checkpoints.put(21, "Halfway");
checkpoints.put(30, "Hill top");
checkpoints.put(42, "Finish");
int[] positions = {17, 35, 0};
for (int km : positions) {
Integer prev = checkpoints.lowerKey(km);
System.out.println("km " + km + ": previous checkpoint " + (prev == null ? "none" : prev + " " + checkpoints.get(prev)));
}
}
}
Output
km 17: previous checkpoint 10 Bridge
km 35: previous checkpoint 30 Hill top
km 0: previous checkpoint none
How It Works
Between two markers. At km 17 the keys below are 0 and 10, and the greatest of them, 10, is the Bridge.
Late in the race. km 35 falls between 30 and 42, so the last checkpoint passed is the Hill top at 30.
Standing on the start line. 0 is the smallest key, and since lowerKey refuses equal keys there is nothing before it: the result is null, printed as "none".
Example 2 — lowerKey() in a Reverse-Ordered Map
Membership levels by points, stored once in natural order and once reversed.
Java
import java.util.Comparator;
import java.util.TreeMap;
public class TreeMapLowerKeyReverse {
public static void main(String[] args) {
TreeMap<Integer, String> natural = new TreeMap<>();
TreeMap<Integer, String> reversed = new TreeMap<>(Comparator.reverseOrder());
int[] points = {10, 20, 30};
String[] names = {"bronze", "silver", "gold"};
for (int i = 0; i < points.length; i++) {
natural.put(points[i], names[i]);
reversed.put(points[i], names[i]);
}
System.out.println("natural lowerKey(20): " + natural.lowerKey(20));
System.out.println("reversed lowerKey(20): " + reversed.lowerKey(20));
System.out.println("descendingMap().lowerKey(20): " + natural.descendingMap().lowerKey(20));
}
}