Example 1 — Display a Sorted Scoreboard Highest-First Without Copying
Use descendingSet() to print top scores high-to-low and reverse a range slice.
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
- Instant reverse display.
scores.descendingSet()prints[92, 85, 78, 64, 55]without touching the original tree. - Chain with range views. Calling
descendingSet()aftertailSet(75, true)reverses only the qualifying scores. - Involution property. Reversing a reversed view brings back the original ascending order.

