Java TreeMap descendingMap() Method

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

Definition

In Java, descendingMap() returns a reverse-order view of a TreeMap: iterating it visits the keys from highest to lowest. Nothing is copied or re-sorted; the view reads the same tree backwards.

The view is live and fully functional. It returns NavigableMap, so it has its own firstKey, floorKey, headMap, and so on, all interpreted in the reversed order. The method was added 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

Call it on the map, then use the result like any NavigableMap.

Java
TreeMap<Integer, String> scores = new TreeMap<>();
NavigableMap<Integer, String> highToLow = scores.descendingMap();
for (Map.Entry<Integer, String> e : highToLow.entrySet()) { /* largest key first */ }
CallWhat it does
map.descendingMap()Live view in reverse key order.
map.descendingMap().firstKey()The original map's highest key.
map.descendingMap().descendingMap()Back to the original (ascending) order.

Since Java 21, TreeMap.reversed() (from SequencedMap) gives the same reverse view. On Java 8 to 17, descendingMap() is the way to go.

Library declarations
Java
// java.util.TreeMap<K, V>
public NavigableMap<K, V> descendingMap()

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

Parameters

ParameterDescription
(none)descendingMap takes no parameters.

Return Value

Return ValueDescription
NavigableMap<K, V>A live view of the same mappings, ordered from highest key to lowest.

Compare with Similar Methods

Ways to go from high to low:

MethodHow it differs
descendingKeySet()Just the keys, in reverse order.
pollLastEntry()Takes entries off the high end, removing them.
lastKey()Just the highest key.
Collections.reverseOrder()Builds a map that is reversed permanently.

Java Examples

Example 1 — Print a Leaderboard From the Top

Scores are stored in natural order; the descending view prints them highest first with a rank.

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

public class TreeMapDescendingMapLeaderboard {
    public static void main(String[] args) {
        TreeMap<Integer, String> scores = new TreeMap<>();
        scores.put(1200, "Ana");
        scores.put(950, "Raj");
        scores.put(1430, "Lee");
        scores.put(800, "Kim");

        int rank = 1;
        for (Map.Entry<Integer, String> e : scores.descendingMap().entrySet()) {
            System.out.println("#" + rank++ + " " + e.getValue() + " " + e.getKey());
        }
    }
}

How It Works

  1. No reverse comparator required. scores keeps ascending order, and only the loop reads it through the descending view.
  2. Rank from a counter. rank++ inside the concatenation prints the current rank and then increments it for the next row.
  3. Same data, two orders. Printing scores directly would still start with 800; the view changes the reading direction, not the map.

Example 2 — Add Through the Reversed View

A put on the view lands in the map, and a remove on the map disappears from the view.

Java
import java.util.NavigableMap;
import java.util.TreeMap;

public class TreeMapDescendingMapWriteThrough {
    public static void main(String[] args) {
        TreeMap<String, Integer> stock = new TreeMap<>();
        stock.put("apple", 5);
        stock.put("cherry", 12);

        NavigableMap<String, Integer> zToA = stock.descendingMap();
        zToA.put("banana", 7);
        System.out.println("view: " + zToA);
        System.out.println("map: " + stock);

        stock.remove("cherry");
        System.out.println("view after map.remove: " + zToA);
    }
}

How It Works

  1. One store, two readers. "banana" was added through zToA, yet it shows in both printouts, slotted between apple and cherry.
  2. Opposite print orders. The view prints Z to A while the map prints A to Z; the entries and values are identical.
  3. Removal is visible instantly. Deleting "cherry" from the map removes it from the view too, with no refresh step.

Example 3 — Navigation Methods on a Descending View

The same three keys, asked the same questions through the map and through its reversed view.

Java
import java.util.NavigableMap;
import java.util.TreeMap;

public class TreeMapDescendingMapFlipped {
    public static void main(String[] args) {
        TreeMap<Integer, String> levels = new TreeMap<>();
        levels.put(10, "low");
        levels.put(20, "mid");
        levels.put(30, "high");
        NavigableMap<Integer, String> down = levels.descendingMap();

        System.out.println("firstKey: map " + levels.firstKey() + ", view " + down.firstKey());
        System.out.println("floorKey(25): map " + levels.floorKey(25) + ", view " + down.floorKey(25));
        System.out.println("headMap(20): map " + levels.headMap(20) + ", view " + down.headMap(20));
    }
}

How It Works

  1. "First" follows the view's order. The descending view starts at 30, so its firstKey() is the map's largest key.
  2. Floor rounds the other way. In the view, "less than or equal" means "at or after in descending order", so floorKey(25) moves up to 30, exactly what ceilingKey(25) gives on the map.
  3. Head means the front of the view. down.headMap(20) holds the keys before 20 in descending order, which is only 30.

Frequently Asked Questions

Loop over map.descendingMap().entrySet(), or over descendingKeySet() if you only need the keys.
No. It is a live view over the same tree, created in constant time.
Yes. put, remove, and clear on the view change the backing map.
Navigation follows the view's reversed order, so floor and ceiling (and lower and higher) swap roles compared with the original map.
Use descendingMap() when you sometimes need the reverse order; build the map with Collections.reverseOrder() when it should always be reversed.

Next: entrySet()

Loop over key/value pairs in key order and edit values.

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

9 people found this page helpful