Collections: NavigableMap.descendingMap() is a view, not a copy

descendingMap() returns a reverse-order view backed by the same TreeMap, so writes to the original are still visible through it. Only the reading order flips; no elements are copied.

Code
NavigableMap<Integer, String> map = new TreeMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
NavigableMap<Integer, String> desc = map.descendingMap();
System.out.println("Descending: " + desc);
map.put(4, "four");
System.out.println("After adding 4 to original: " + desc);
Output
Descending: {3=three, 2=two, 1=one}
After adding 4 to original: {4=four, 3=three, 2=two, 1=one}
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms