Java TreeMap descendingMap() Method
Example
Print a sorted map in reverse order:
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, String> capitalCities = new TreeMap<String, String>();
capitalCities.put("England", "London");
capitalCities.put("Austria", "Wien");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities.descendingMap());
}
}
Definition and Usage
The descendingMap() method returns the entries of the map in reverse (descending) order, from the last (highest) key to the first (lowest) key.
The returned map is a view of the original map (it shows the same entries, it is not a copy). If you change one of them, the other one changes too.
Syntax
public NavigableMap<K,V> descendingMap()
K refers to the data type of the keys in the map, and V refers to the data type of the values.
Parameter Values
None.
Technical Details
| Returns: | A map with the same entries, in reverse order. |
|---|
Related Pages
❮ TreeMap Methods