putLast(key, value) is the second positioning method that Java 21's SequencedMap interface introduced. On an insertion-ordered map it puts the mapping at the end (moving it there if the key exists). A TreeMap implements the interface but always throws UnsupportedOperationException from putLast.
A key's place in a TreeMap follows from comparing it with the other keys, so "put this last" is a request the map cannot honour. To make an entry last, give it a key that sorts last (for example the next number) and call put().
An instance method of java.util.TreeMap, added in Java 21 by the SequencedMap 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. Requires Java 21 or newer.
Syntax
Valid Java 21 code that always ends in an exception on a TreeMap.
Java
TreeMap<Integer, String> log = new TreeMap<>();
log.putLast(3, "logout"); // throws UnsupportedOperationException
log.put(3, "logout"); // works; 3 sorts after 1 and 2
Call
What it does
treeMap.putLast(k, v)
Always throws UnsupportedOperationException.
treeMap.reversed().putLast(k, v)
The reversed view is sorted too, so it throws as well.
linkedHashMap.putLast(k, v)
Works: inserts or moves the mapping to the end.
treeMap.lastEntry()
Reads the entry that currently sorts last.
putLast and putFirst() are the only SequencedMap methods a TreeMap rejects; firstEntry, lastEntry, pollFirstEntry, pollLastEntry, and reversed all work.
Library declarations
Java
// java.util.TreeMap<K, V> (Java 21+)
public V putLast(K k, V v) // always throws UnsupportedOperationException
// Declared in SequencedMap<K, V> (Java 21); SortedMap and TreeMap reject it
Parameters
Parameter
Description
k (K)
The key. Not used: the method throws first.
v (V)
The value. Not used either.
Return Value
Return Value
Description
V
The declared type; a TreeMap never returns from this call.
Still a sorted map.levels.reversed() is the same as descendingMap(): sorted, just backwards, so putLast on it throws.
put through the view.rev.put("silver", 2) is allowed and writes into levels, because the view has no storage of its own.
One map, two directions. The view prints silver, gold, bronze (Z to A), the original bronze, gold, silver (A to Z).
Example 3 — Copy Into a LinkedHashMap for a Hand-Made Order
Java 21: keep the sorted TreeMap, and build a shopping order in a LinkedHashMap where putLast and putFirst work.
Java
import java.util.LinkedHashMap;
import java.util.TreeMap;
public class TreeMapPutLastLinkedCopy {
public static void main(String[] args) {
TreeMap<String, Integer> sorted = new TreeMap<>();
sorted.put("pasta", 9);
sorted.put("bread", 3);
sorted.put("milk", 2);
LinkedHashMap<String, Integer> shoppingOrder = new LinkedHashMap<>(sorted);
shoppingOrder.putLast("bread", 4);
shoppingOrder.putFirst("pasta", 9);
System.out.println("sorted: " + sorted);
System.out.println("my order: " + shoppingOrder);
}
}
Output
sorted: {bread=3, milk=2, pasta=9}
my order: {pasta=9, milk=2, bread=4}
How It Works
The copy starts sorted.new LinkedHashMap<>(sorted) takes the entries in the TreeMap's order: bread, milk, pasta.
putLast moves and updates. bread already exists, so the LinkedHashMap moves it to the end and stores the new value 4.
The source stays sorted. pasta is moved to the front of the copy only; the TreeMap still prints bread=3 first.
Frequently Asked Questions
The comparator decides where each key goes, so a TreeMap cannot put a mapping at the end on request. Java 21 specifies UnsupportedOperationException for it.
Give it a key that sorts after all others, such as lastKey() + 1 for numbers, and call put().
No. reversed() returns the descending view, which is also sorted and also rejects putLast and putFirst.
It inserts the mapping, or replaces the value and moves an existing key, so that it becomes the last entry in the map's order.
Yes. SequencedMap and its putFirst and putLast methods were added in Java 21; earlier versions will not compile the call.