Home Java TreeMap Methods descendingKeySet() Java TreeMap descendingKeySet() Method Definition In Java, descendingKeySet() returns the keys of a TreeMap as a NavigableSet ordered from highest to lowest . It is equivalent to descendingMap().navigableKeySet().
The set is a live view backed by the map: removing a key from the set removes the whole mapping, and new keys added to the map appear in the set. Adding keys through the set is not possible, because a set element has no value to store. The method arrived in Java 6.
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 Use it wherever you need the keys largest first.
TreeMap<String, String> posts = new TreeMap<>();
NavigableSet<String> newestFirst = posts.descendingKeySet();
for (String date : newestFirst) { /* latest date first */ } Call What it does map.descendingKeySet()Keys from highest to lowest, as a live set. map.navigableKeySet()The same keys in ascending order. map.descendingMap()Keys and values in reverse order.
Because the set is navigable, it supports first(), floor(), headSet(), and friends, all in reversed order: first() is the map's largest key.
Library declarations // java.util.TreeMap<K, V>
public NavigableSet<K> descendingKeySet()
// Declared in NavigableMap<K, V> (Java 6) Parameters Parameter Description (none) descendingKeySet takes no parameters.
Return Value Return Value Description NavigableSet<K>A live, reverse-ordered view of the map's keys.
Important
add and addAll on the set throw UnsupportedOperationException. remove, pollFirst, removeIf, and clear work and delete mappings from the map. Changing the map structurally while iterating the set throws ConcurrentModificationException.
Compare with Similar Methods Key views compared:
Java Examples 1. Newest first 2. Remove works, add does not 3. pollFirst on a reversed set Example 1 — List Blog Posts From Newest to Oldest Dates are the keys; the descending key set drives a newest-first loop.
import java.util.TreeMap;
public class TreeMapDescendingKeySetNewest {
public static void main(String[] args) {
TreeMap<String, String> posts = new TreeMap<>();
posts.put("2026-09-02", "Hello Java");
posts.put("2026-11-03", "Streams");
posts.put("2026-10-15", "Generics");
for (String date : posts.descendingKeySet()) {
System.out.println(date + " " + posts.get(date));
}
System.out.println("newest: " + posts.descendingKeySet().first());
}
} 2026-11-03 Streams
2026-10-15 Generics
2026-09-02 Hello Java
newest: 2026-11-03 How It Works Reverse chronological order. ISO dates sort like calendar dates, so the descending set starts with November and ends with September.Values by lookup. The set holds only keys, so each title is fetched with posts.get(date); use descendingMap() if you want key and value together.First of the reversed set. first() on the descending set is the latest date, the same key lastKey() would give on the map.Example 2 — Release a Room Through the Key Set Removing a key from the set deletes the room from the map; adding one is rejected.
import java.util.NavigableSet;
import java.util.TreeMap;
public class TreeMapDescendingKeySetRemoveAdd {
public static void main(String[] args) {
TreeMap<Integer, String> rooms = new TreeMap<>();
rooms.put(101, "Ito");
rooms.put(102, "Sam");
rooms.put(201, "Noa");
rooms.put(202, "Kai");
NavigableSet<Integer> topDown = rooms.descendingKeySet();
System.out.println("view: " + topDown);
topDown.remove(201);
System.out.println("map after view.remove(201): " + rooms);
try {
topDown.add(301);
} catch (UnsupportedOperationException e) {
System.out.println("view.add(301): " + e.getClass().getSimpleName());
}
}
} view: [202, 201, 102, 101]
map after view.remove(201): {101=Ito, 102=Sam, 202=Kai}
view.add(301): UnsupportedOperationException How It Works Top floor first. The set lists room numbers from 202 down to 101.Removing a key removes the guest. remove(201) on the set deletes the mapping 201=Noa from rooms.No value, no add. A new room needs a guest name, which a set cannot supply, so add(301) throws; use rooms.put instead.Example 3 — pollFirst() Removes the Largest Key On a descending set, "first" is the highest key, so pollFirst() takes the latest edit off the map.
import java.util.NavigableSet;
import java.util.TreeMap;
public class TreeMapDescendingKeySetPoll {
public static void main(String[] args) {
TreeMap<Integer, String> edits = new TreeMap<>();
edits.put(1, "type");
edits.put(2, "bold");
edits.put(3, "link");
NavigableSet<Integer> latestFirst = edits.descendingKeySet();
System.out.println("pollFirst(): " + latestFirst.pollFirst());
System.out.println("remaining: " + edits);
System.out.println("view first: " + latestFirst.first() + ", view last: " + latestFirst.last());
}
} pollFirst(): 3
remaining: {1=type, 2=bold}
view first: 2, view last: 1 How It Works First in reverse is the maximum. The set is ordered 3, 2, 1, so pollFirst() returns 3.Polling deletes from the map. Edit 3 ("link") is gone from edits, just as if pollLastEntry() had been called on the map.The view adjusts. With 3 removed, the descending set now begins at 2 and ends at 1.Frequently Asked Questions What does TreeMap descendingKeySet() return? A NavigableSet of the map's keys in descending order. It is a live view, not a copy.
Can I add keys through descendingKeySet()? No. add() throws UnsupportedOperationException because there is no value to store. Use put on the map.
Does removing from descendingKeySet() change the map? Yes. Removing a key from the set removes its whole mapping from the TreeMap.
What is the difference between descendingKeySet() and descendingMap()? descendingKeySet() gives only the keys; descendingMap() gives keys and values, both in reverse order.
What does pollFirst() do on a descending key set? It removes and returns the largest key of the map, because that key comes first in descending order.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion