Java TreeSet descendingSet() Method

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

Definition

In Java, descendingSet() returns a live reversed NavigableSet view of the TreeSet, ordered from largest to smallest (the opposite of the set's current comparator).

Because it is only a view, creating it takes O(1) time and uses no extra copy of the elements. Calling pollFirst(), add(), or remove() on the reversed view updates the original TreeSet directly.

An instance method of java.util.TreeSet (Java 6+ (added via NavigableSet in JDK 1.6)): a red-black tree NavigableSet backed by a TreeMap that keeps unique elements sorted in O(log n) time and is not thread-safe. Added in Java 6 via NavigableSet. Java 21 also adds reversed() via SequencedSet, while this guide sticks to Java 8+ methods.

Syntax

Call descendingSet() to view and navigate the set in reverse order.

Java
NavigableSet<Integer> asc = new TreeSet<>(Arrays.asList(10, 20, 30));
NavigableSet<Integer> desc = asc.descendingSet();   // [30, 20, 10]
int highest = desc.first();                         // 30
CallWhat it does
set.descendingSet()Live reversed NavigableSet view.
set.descendingSet().first()Reads the largest element (same as set.last()).
set.descendingSet().pollFirst()Pops the largest element from the original set.
set.descendingSet().descendingSet()Restores the original forward order.

If you only want a one-time reverse loop without calling reversed view methods, descendingIterator() is slightly lighter.

Library declarations
Java
// java.util.TreeSet<E>  (since Java 6)
public NavigableSet<E> descendingSet()

// Declared in NavigableSet<E>

Parameters

ParameterDescription
NonedescendingSet() takes no parameters.

Return Value

Return ValueDescription
NavigableSet<E>A live view of the set in reverse order.

Compare with Similar Methods

Ways to reverse or iterate backward over a TreeSet:

MethodHow it differs
descendingIterator()Steps backward with an Iterator when you only need a loop.
new TreeSet<>(Comparator.reverseOrder())Creates a separate descending copy instead of a live view.
subSet(a, b)Can be chained with descendingSet() to view a range backward.
pollLast()Pops the highest element directly without creating a view.

Java Examples

Example 1 — Display a Sorted Scoreboard Highest-First Without Copying

Use descendingSet() to print top scores high-to-low and reverse a range slice.

Java
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;

public class TreeSetDescendingLeaderboard {
    public static void main(String[] args) {
        NavigableSet<Integer> scores = new TreeSet<>(Arrays.asList(64, 92, 78, 85, 55));
        NavigableSet<Integer> highToLow = scores.descendingSet();

        System.out.println("ascending:        " + scores);
        System.out.println("descending view:  " + highToLow);
        System.out.println("top range >= 75:  " + scores.tailSet(75, true).descendingSet());
        System.out.println("double reversed:  " + highToLow.descendingSet());
    }
}

How It Works

  1. Instant reverse display. scores.descendingSet() prints [92, 85, 78, 64, 55] without touching the original tree.
  2. Chain with range views. Calling descendingSet() after tailSet(75, true) reverses only the qualifying scores.
  3. Involution property. Reversing a reversed view brings back the original ascending order.

Example 2 — Mutate Both Ways Between TreeSet and descendingSet()

Pop the top item through descendingSet().pollFirst(), add an element to the original set, and inspect both.

Java
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;

public class TreeSetDescendingSync {
    public static void main(String[] args) {
        NavigableSet<String> tags = new TreeSet<>(Arrays.asList("alpha", "beta", "gamma"));
        NavigableSet<String> desc = tags.descendingSet();

        String removed = desc.pollFirst();
        tags.add("delta");

        System.out.println("popped via desc.pollFirst(): " + removed);
        System.out.println("original tags now:           " + tags);
        System.out.println("descending view now:         " + desc);
    }
}

How It Works

  1. Reversed front matches original tail. Alphabetically "gamma" comes last in tags, so desc.pollFirst() pops "gamma" off both views.
  2. Original add appears reversed. Inserting "delta" into tags puts "delta" first in desc ahead of "beta" and "alpha".
  3. One shared backing tree. Both views reflect every addition and deletion live.

Example 3 — How ceiling(), floor(), and comparator() Flip in descendingSet()

Verify that descendingSet().comparator() is non-null and that ceiling()/floor() swap directions.

Java
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;

public class TreeSetDescendingNav {
    public static void main(String[] args) {
        NavigableSet<Integer> asc = new TreeSet<>(Arrays.asList(10, 20, 30, 40));
        NavigableSet<Integer> desc = asc.descendingSet();

        System.out.println("asc.comparator() == null?  " + (asc.comparator() == null));
        System.out.println("desc.comparator() == null? " + (desc.comparator() == null));
        System.out.println("asc.floor(25):             " + asc.floor(25));
        System.out.println("desc.ceiling(25):          " + desc.ceiling(25));
        System.out.println("desc.subSet(35, 15):       " + desc.subSet(35, 15));
    }
}

How It Works

  1. Comparator flips to non-null. asc uses natural ordering (null), whereas desc.comparator() returns a reverse comparator.
  2. ceiling becomes floor. In descending order [40, 30, 20, 10], the first element at or after 25 is 20, matching asc.floor(25).
  3. subSet bounds reverse too. On desc, the lower bound comes first in descending order (35 before 15), returning [30, 20].

Frequently Asked Questions

No. It returns a live reversed NavigableSet view backed by the original TreeSet, so changes in one show immediately in the other.
descendingSet() returns a full NavigableSet view supporting contains, add, subSet, and loops; descendingIterator() returns only a single-pass Iterator from largest to smallest.
Even if the original TreeSet used natural ordering (comparator() == null), descendingSet() orders elements in reverse, so comparator() returns a reverse Comparator.
Because in descending order 30 comes before 10. Pass the bounds in the descending view order: descendingSet().subSet(30, 10).
Yes. tailSet(), headSet(), and subSet() return NavigableSet (when using the two/four-arg forms or cast), so you can chain .descendingSet() onto them.

Next: first()

Read the smallest (lowest) element without removing it; throws if empty.

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

15 people found this page helpful