Java TreeMap lowerKey() Method

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

Definition

In Java, lowerKey(key) returns the greatest key strictly less than key 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.

Java
TreeMap<Integer, String> marks = new TreeMap<>();
marks.put(10, "Bridge");
marks.put(21, "Halfway");
Integer prev = marks.lowerKey(21);   // 10
Integer none = marks.lowerKey(10);   // null
CallWhat it does
map.lowerKey(k)Greatest key < k, or null.
map.floorKey(k)Greatest key ≤ k (keeps an exact match).
map.lowerEntry(k)The previous key together with its value.

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

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

Return Value

Return ValueDescription
KThe greatest key that compares strictly less than key.
nullNo key comes before key, or the map is empty.

Compare with Similar Methods

How lowerKey relates to its neighbours:

MethodHow it differs
floorKey(k)Same direction but includes k itself.
higherKey(k)Strictly greater: the key after k.
lowerEntry(k)Returns a key/value snapshot.
headMap(k)Every key strictly below k.

Java Examples

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)));
        }
    }
}

How It Works

  1. Between two markers. At km 17 the keys below are 0 and 10, and the greatest of them, 10, is the Bridge.
  2. Late in the race. km 35 falls between 30 and 42, so the last checkpoint passed is the Hill top at 30.
  3. 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));
    }
}

How It Works

  1. Lower by number. In the natural map 10 comes just before 20, so that is the answer.
  2. Lower by position. The reversed map is ordered 30, 20, 10; "strictly less" means "earlier in that order", which is 30.
  3. Two routes to the same answer. A descending view of the natural map orders its keys the same way as the reversed map, so its lowerKey(20) is also 30.

Example 3 — Chapter Start Pages: floorKey() and lowerKey() Compared

Keys are the first page of each chapter; both methods are asked about the same pages.

Java
import java.util.TreeMap;

public class TreeMapLowerKeyVsFloor {
    public static void main(String[] args) {
        TreeMap<Integer, String> chapters = new TreeMap<>();
        chapters.put(1, "Getting started");
        chapters.put(15, "Variables");
        chapters.put(42, "Loops");
        chapters.put(60, "Methods");

        int[] pages = {42, 50, 1};
        for (int page : pages) {
            System.out.println("page " + page + ": floorKey=" + chapters.floorKey(page) + ", lowerKey=" + chapters.lowerKey(page));
        }
    }
}

How It Works

  1. On a chapter's first page they disagree. Page 42 is a key: floorKey keeps it (the Loops chapter), while lowerKey jumps back to 15, the chapter before.
  2. Inside a chapter they agree. Page 50 is not a key, so "at most 50" and "below 50" both lead to 42.
  3. Page 1 has nothing before it. floorKey(1) is 1 itself, but the strict version finds no smaller key and returns null.

Frequently Asked Questions

The greatest key strictly less than the argument, or null when no key is smaller.
floorKey() returns the argument itself if it is a key; lowerKey() never does and returns the key before it.
Your map uses a reverse comparator. "Lower" means earlier in the map's order, which is the larger number when the order is reversed.
null, because no key comes before the first one.
Start from lastKey() and repeat k = map.lowerKey(k) until it is null, or simply iterate descendingMap().

Next: merge()

Combine a new value with the old one, ideal for counters.

merge() 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.

8 people found this page helpful