Java TreeMap lowerEntry() Method

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

Definition

In Java, lowerEntry(key) returns the mapping with the greatest key strictly less than key, or null if no key comes before it. Passing a key that is in the map gives you its predecessor.

The result is a read-only snapshot of that mapping. Like the other navigation methods it was added in Java 6 with NavigableMap 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 a key to get the mapping just before it.

Java
TreeMap<Integer, String> versions = new TreeMap<>();
versions.put(1, "draft");
versions.put(2, "review");
Map.Entry<Integer, String> prev = versions.lowerEntry(2);   // 1=draft
Map.Entry<Integer, String> none = versions.lowerEntry(1);   // null
CallWhat it does
map.lowerEntry(k)Entry with the greatest key < k, or null.
map.floorEntry(k)Includes k's own entry.
map.lowerKey(k)Only the previous key.

Calling lowerEntry inside a loop over entrySet() is safe: it only reads the map, so it does not trigger ConcurrentModificationException.

Library declarations
Java
// java.util.TreeMap<K, V>
public Map.Entry<K, V> lowerEntry(K key)

// Declared in NavigableMap<K, V> (Java 6)

Parameters

ParameterDescription
key (K)The key (or probe value) whose predecessor you want.

Return Value

Return ValueDescription
Map.Entry<K, V>Snapshot of the mapping with the greatest key < key.
nullNothing comes before key, or the map is empty.

Compare with Similar Methods

Choosing a "before" lookup:

MethodHow it differs
floorEntry(k)Returns k's own entry when present.
higherEntry(k)The entry after k.
lowerKey(k)Key only.
descendingMap()Iterate everything from high to low.

Java Examples

Example 1 — Who Is Each Runner Chasing?

Finishing times in seconds are the keys; the lower entry of a time is the runner who finished just before.

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

public class TreeMapLowerEntryRace {
    public static void main(String[] args) {
        TreeMap<Integer, String> results = new TreeMap<>();
        results.put(3720, "Bo");
        results.put(3605, "Ali");
        results.put(3902, "Di");
        results.put(3810, "Cy");

        for (Map.Entry<Integer, String> e : results.entrySet()) {
            Map.Entry<Integer, String> ahead = results.lowerEntry(e.getKey());
            if (ahead == null) {
                System.out.println(e.getValue() + ": winner");
            } else {
                System.out.println(e.getValue() + ": " + (e.getKey() - ahead.getKey()) + " s behind " + ahead.getValue());
            }
        }
    }
}

How It Works

  1. The fastest has no one ahead. 3605 is the smallest time, its lower entry is null, and Ali is declared the winner.
  2. Strictly earlier times only. For Bo's 3720 the method skips Bo's own key and returns 3605=Ali, a gap of 115 seconds.
  3. Reading while looping is fine. lowerEntry does not modify the tree, so calling it inside the entrySet() loop never upsets the iterator.

Example 2 — Step Back Through Document Versions

Start at lastEntry() and keep asking for the entry before the current key.

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

public class TreeMapLowerEntryHistory {
    public static void main(String[] args) {
        TreeMap<Integer, String> versions = new TreeMap<>();
        versions.put(1, "draft");
        versions.put(2, "review");
        versions.put(3, "typo fix");
        versions.put(4, "final");

        for (Map.Entry<Integer, String> e = versions.lastEntry(); e != null; e = versions.lowerEntry(e.getKey())) {
            System.out.println("v" + e.getKey() + " " + e.getValue());
        }
        System.out.println("undo target from v3: " + versions.lowerEntry(3));
    }
}

How It Works

  1. Start at the top. The loop variable begins as the snapshot of version 4, the highest key.
  2. Each step goes one key down. lowerEntry(e.getKey()) returns the predecessor, so the loop prints 4, 3, 2, 1 and stops when version 1 has no predecessor.
  3. Undo in one call. Asking for the entry before 3 gives 2=review, the version an undo from v3 would restore.

Example 3 — Who Handed Over the Current Shift?

floorEntry finds the shift on duty; lowerEntry of its start hour finds the shift before, wrapping to the night shift.

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

public class TreeMapLowerEntryHandover {
    public static void main(String[] args) {
        TreeMap<Integer, String> shifts = new TreeMap<>();
        shifts.put(6, "Ana");
        shifts.put(14, "Ben");
        shifts.put(22, "Cleo");

        int[] hours = {14, 15, 6};
        for (int h : hours) {
            Map.Entry<Integer, String> current = shifts.floorEntry(h);
            Map.Entry<Integer, String> previous = shifts.lowerEntry(current.getKey());
            if (previous == null) {
                previous = shifts.lastEntry();
            }
            System.out.println("hour " + h + ": " + current.getValue() + " on duty, took over from " + previous.getValue());
        }
    }
}

How It Works

  1. Floor for "now", lower for "before". At hour 14 floorEntry returns Ben's own shift, and lowerEntry(14) steps back to Ana.
  2. Same answer mid-shift. Hour 15 still floors to 14, so the previous shift is again Ana; using the shift's start key keeps the result stable.
  3. Wrap around midnight. Ana's 6:00 shift is the first key, the lower entry is null, and the code borrows lastEntry(), Cleo's night shift.

Frequently Asked Questions

The mapping with the greatest key strictly less than the argument, or null when no key is smaller.
floorEntry() returns the argument's own entry when the key exists; lowerEntry() always returns the entry before it.
Yes. It only reads the map, so the iterator is not invalidated.
null, because nothing comes before the lowest key.
No. The entry is a snapshot and setValue throws UnsupportedOperationException; use put instead.

Next: lowerKey()

Find the largest key strictly less than a value.

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

7 people found this page helpful