Java TreeMap floorEntry() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

Definition

In Java, floorEntry(key) returns the mapping whose key is the greatest key less than or equal to key, 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
CallWhat 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

ParameterDescription
key (K)The probe value; it need not be present in the map.

Return Value

Return ValueDescription
Map.Entry<K, V>Snapshot of the mapping with the greatest key ≤ key.
nullAll keys are greater than key, or the map is empty.

Compare with Similar Methods

Entry lookups around a probe:

MethodHow it differs
ceilingEntry(k)Rounds up: least key ≥ k.
lowerEntry(k)Skips an exact match.
floorKey(k)Key only, same position.
headMap(k, true)All entries up to and including k.

Java Examples

Example 1 — Turn Test Scores Into Letter Grades

Each key is the lowest score for a grade; the floor entry of a score is its grade.

Java
import java.util.Map;
import java.util.TreeMap;

public class TreeMapFloorEntryGrades {
    public static void main(String[] args) {
        TreeMap<Integer, String> grades = new TreeMap<>();
        grades.put(0, "F");
        grades.put(60, "D");
        grades.put(70, "C");
        grades.put(80, "B");
        grades.put(90, "A");

        int[] scores = {59, 70, 85, 100};
        for (int score : scores) {
            Map.Entry<Integer, String> g = grades.floorEntry(score);
            System.out.println("score " + score + ": " + g.getValue() + " (from " + g.getKey() + ")");
        }
    }
}

How It Works

  1. Just below a boundary. 59 is under 60, so the greatest key not above it is 0 and the grade is F.
  2. Exactly on a boundary. 70 is a key; "less than or equal" includes it, so the score earns a C rather than a D.
  3. Five keys cover every score. 85 falls to 80 (B) and 100 to 90 (A); the table never needs an entry per possible score.

Example 2 — Shipping Price by Parcel Weight

Courier bands start at 1000 g; lighter parcels have no floor entry and go by letter post.

Java
import java.util.Map;
import java.util.TreeMap;

public class TreeMapFloorEntryShipping {
    public static void main(String[] args) {
        TreeMap<Integer, Double> courier = new TreeMap<>();
        courier.put(1000, 4.99);
        courier.put(5000, 9.99);
        courier.put(20000, 24.99);

        int[] parcels = {500, 1000, 7300};
        for (int grams : parcels) {
            Map.Entry<Integer, Double> band = courier.floorEntry(grams);
            if (band == null) {
                System.out.println(grams + " g: letter post");
            } else {
                System.out.println(grams + " g: " + band.getValue() + " (band from " + band.getKey() + " g)");
            }
        }
    }
}

How It Works

  1. Lighter than every band. No key is at or below 500, so floorEntry returns null and the parcel goes by letter post.
  2. The band starts at its key. A parcel of exactly 1000 g matches the first key and pays 4.99.
  3. Between bands. 7300 g lies between 5000 and 20000; the floor is the 5000 g band, so the price is 9.99.

Example 3 — Convert Numbers to Roman Numerals

Repeatedly take the largest Roman value that fits; floorEntry finds it.

Java
import java.util.Map;
import java.util.TreeMap;

public class TreeMapFloorEntryRoman {
    static final TreeMap<Integer, String> ROMAN = new TreeMap<>();

    static String toRoman(int n) {
        Map.Entry<Integer, String> e = ROMAN.floorEntry(n);
        if (n == e.getKey()) {
            return e.getValue();
        }
        return e.getValue() + toRoman(n - e.getKey());
    }

    public static void main(String[] args) {
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        for (int i = 0; i < values.length; i++) {
            ROMAN.put(values[i], symbols[i]);
        }
        System.out.println("1994 = " + toRoman(1994));
        System.out.println("2026 = " + toRoman(2026));
        System.out.println("49 = " + toRoman(49));
    }
}

How It Works

  1. Largest piece first. For 1994 the floor entry is 1000=M, leaving 994, whose floor is 900=CM, then 90=XC for 94.
  2. Stop on an exact key. The remainder 4 is itself a key, so the recursion ends with IV and the result reads MCMXCIV.
  3. Subtractive pairs as keys. Storing 40=XL and 9=IX means 49 becomes XLIX directly, instead of the wrong XXXXVIIII.

Frequently Asked Questions

The mapping with the greatest key less than or equal to the argument, or null if every key is larger.
When you also need the value, such as the grade or price of a band. It saves a second get() call.
No. It is a snapshot, and setValue throws UnsupportedOperationException. Use put(entry.getKey(), newValue).
Step tables: tax bands, grade boundaries, shipping weights, price tiers, or any value that belongs to the range starting at the nearest key below it.
Yes. If the argument is a key, its own entry is returned. lowerEntry() is the strict version.

Next: floorKey()

Find the largest key that is less than or equal to a value.

floorKey() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful