Java TreeMap lastEntry() Method

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

Definition

In Java, lastEntry() returns the mapping with the highest key in a TreeMap as a Map.Entry, or null when the map is empty. It is the null-safe, key-plus-value companion of lastKey().

The entry is a snapshot: later changes to the map are not reflected in it, and setValue is not supported. Added in Java 6 with NavigableMap, it 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

Call it without arguments and read getKey() and getValue().

Java
TreeMap<Double, String> jumps = new TreeMap<>();
jumps.put(7.45, "Ines");
jumps.put(8.02, "Rae");
Map.Entry<Double, String> best = jumps.lastEntry();   // 8.02=Rae
CallWhat it does
map.lastEntry()Highest entry, or null when empty.
map.lastKey()Highest key only; throws when empty.
map.pollLastEntry()Highest entry, removed from the map.

Because TreeMap sorts by key, "last" means largest key, never "most recently added". If two values share a key, the later put replaced the earlier one, so only one of them can be the last entry.

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

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

Parameters

ParameterDescription
(none)lastEntry takes no parameters.

Return Value

Return ValueDescription
Map.Entry<K, V>A snapshot of the mapping with the highest key.
nullThe map is empty.

Compare with Similar Methods

High-end methods compared:

MethodHow it differs
lastKey()Returns the key and throws NoSuchElementException when empty.
firstEntry()The lowest entry instead.
pollLastEntry()Returns the highest entry and removes it.
floorEntry(k)Highest entry whose key is at most k.

Java Examples

Example 1 — Find the Longest Jump

Distances in metres are Double keys; the last entry is the best jump and its athlete.

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

public class TreeMapLastEntryRecord {
    public static void main(String[] args) {
        TreeMap<Double, String> jumps = new TreeMap<>();
        jumps.put(7.45, "Ines");
        jumps.put(8.02, "Rae");
        jumps.put(7.91, "Tom");

        Map.Entry<Double, String> best = jumps.lastEntry();
        System.out.println("best jump: " + best);
        System.out.println("winner: " + best.getValue() + " with " + best.getKey() + " m");
    }
}

How It Works

  1. Double keys sort numerically. 7.45, 7.91, and 8.02 are ordered by value, putting Rae's 8.02 at the right end of the tree.
  2. Winner and distance together. getValue() names the athlete and getKey() gives the distance from the very same snapshot.
  3. Ties would collide. Two athletes with exactly 8.02 would share one key, so the second put would replace the first; a list per key avoids losing anyone.

Example 2 — A Saved lastEntry() Does Not See Later Updates

Firmware version 12 is promoted from beta to stable after the snapshot was taken.

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

public class TreeMapLastEntryStale {
    public static void main(String[] args) {
        TreeMap<Integer, String> firmware = new TreeMap<>();
        firmware.put(10, "stable");
        firmware.put(11, "stable");
        firmware.put(12, "beta");

        Map.Entry<Integer, String> newest = firmware.lastEntry();
        System.out.println("snapshot: " + newest);

        firmware.put(12, "stable");
        System.out.println("map after put: " + firmware);
        System.out.println("old snapshot: " + newest + ", fresh call: " + firmware.lastEntry());
    }
}

How It Works

  1. Copied at call time. newest stores key 12 and the value "beta" as they were when lastEntry() ran.
  2. The map moves on. put(12, "stable") replaces the value inside the tree, which the printed map shows.
  3. Old copy, new answer. The saved entry still says beta while a second lastEntry() reports stable, so call again whenever you need current data.

Example 3 — Youngest, Oldest, and the Age Span

firstEntry() and lastEntry() together describe the range of a group.

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

public class TreeMapLastEntrySpan {
    public static void main(String[] args) {
        TreeMap<Integer, String> byAge = new TreeMap<>();
        byAge.put(34, "Mia");
        byAge.put(8, "Leo");
        byAge.put(71, "Gus");
        byAge.put(45, "Ola");

        Map.Entry<Integer, String> oldest = byAge.lastEntry();
        Map.Entry<Integer, String> youngest = byAge.firstEntry();
        System.out.println("oldest: " + oldest.getValue() + " (" + oldest.getKey() + ")");
        System.out.println("youngest: " + youngest.getValue() + " (" + youngest.getKey() + ")");
        System.out.println("age span: " + (oldest.getKey() - youngest.getKey()) + " years");
    }
}

How It Works

  1. Largest age at the end. With ages as keys, the last entry belongs to Gus at 71.
  2. The other end for free. firstEntry() yields Leo at 8, the smallest key, in the same O(log n) time.
  3. Arithmetic on keys. Subtracting the two Integer keys (auto-unboxed) gives the span of 63 years; with an empty map both entries would be null, so real code checks first.

Frequently Asked Questions

The mapping with the highest key as a Map.Entry, or null if the map is empty.
lastEntry() returns key and value and gives null when empty; lastKey() returns only the key and throws NoSuchElementException when empty.
No. The entry is a snapshot and setValue throws UnsupportedOperationException. Use put with the same key.
No. It is the entry with the largest key. TreeMap keeps sorted order, not insertion order.
No. pollLastEntry() is the version that returns and removes the highest entry.

Next: lastKey()

Read the largest key (throws when the map is empty).

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

14 people found this page helpful