Java TreeMap descendingKeySet() Method

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

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.

Java
TreeMap<String, String> posts = new TreeMap<>();
NavigableSet<String> newestFirst = posts.descendingKeySet();
for (String date : newestFirst) { /* latest date first */ }
CallWhat 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
// java.util.TreeMap<K, V>
public NavigableSet<K> descendingKeySet()

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

Parameters

ParameterDescription
(none)descendingKeySet takes no parameters.

Return Value

Return ValueDescription
NavigableSet<K>A live, reverse-ordered view of the map's keys.

Compare with Similar Methods

Key views compared:

MethodHow it differs
navigableKeySet()Ascending order, same live behaviour.
descendingMap()Reverse view that includes the values.
keySet()Ascending keys typed as a plain Set.
lastKey()Only the largest key.

Java Examples

Example 1 — List Blog Posts From Newest to Oldest

Dates are the keys; the descending key set drives a newest-first loop.

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

How It Works

  1. Reverse chronological order. ISO dates sort like calendar dates, so the descending set starts with November and ends with September.
  2. 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.
  3. 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.

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

How It Works

  1. Top floor first. The set lists room numbers from 202 down to 101.
  2. Removing a key removes the guest. remove(201) on the set deletes the mapping 201=Noa from rooms.
  3. 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.

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

How It Works

  1. First in reverse is the maximum. The set is ordered 3, 2, 1, so pollFirst() returns 3.
  2. Polling deletes from the map. Edit 3 ("link") is gone from edits, just as if pollLastEntry() had been called on the map.
  3. The view adjusts. With 3 removed, the descending set now begins at 2 and ends at 1.

Frequently Asked Questions

A NavigableSet of the map's keys in descending order. It is a live view, not a copy.
No. add() throws UnsupportedOperationException because there is no value to store. Use put on the map.
Yes. Removing a key from the set removes its whole mapping from the TreeMap.
descendingKeySet() gives only the keys; descendingMap() gives keys and values, both in reverse order.
It removes and returns the largest key of the map, because that key comes first in descending order.

Next: descendingMap()

A live reversed view of the whole map.

descendingMap() 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