In Java, floorEntry(key) returns the mapping whose key is the greatest key less than or equal tokey, or null if no such key exists. It is floorKey() plus the value.
This is the classic lookup for step tables: store where each band starts, and floorEntry finds the band for any value in one O(log n) search. The result is a read-only snapshot. The method was added in Java 6 with NavigableMap.
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 a probe; get back the band it falls into.
Java
TreeMap<Integer, String> grades = new TreeMap<>();
grades.put(0, "F");
grades.put(60, "D");
grades.put(90, "A");
Map.Entry<Integer, String> g = grades.floorEntry(75); // 60=D
Call
What it does
map.floorEntry(k)
Entry with the greatest key ≤ k, or null.
map.floorKey(k)
Just that key.
map.lowerEntry(k)
Entry with the greatest key strictly below k.
Adding a key for the very bottom of the range (such as 0) means floorEntry can never return null for valid input, which keeps the calling code free of null checks.
Library declarations
Java
// java.util.TreeMap<K, V>
public Map.Entry<K, V> floorEntry(K key)
// Declared in NavigableMap<K, V> (Java 6)
Parameters
Parameter
Description
key (K)
The probe value; it need not be present in the map.
Return Value
Return Value
Description
Map.Entry<K, V>
Snapshot of the mapping with the greatest key ≤ key.
null
All keys are greater than key, or the map is empty.