Let’s learn TreeMap descendingMap() method in java.
TreeMap descendingMap() method in java
descendingMap() method of TreeMap class returns a reverse order view of the mappings contained in this map.
The descending map is backed by this map, so changes to the map are reflected in the descending map, and vice-versa.
Syntax:
public NavigableMap<K, V> descendingMap()
Parameters:
descendingMap() do not accept any parameters.
Returns:
a reverse order view of this map.
Now let’s see example on TreeMap descendingMap() method.
import java.util.NavigableMap; import java.util.TreeMap; public class TreeMapDescendingMapMethodExample { public static void main(String[] args) { TreeMap<Integer, String> tm = new TreeMap<Integer, String>(); tm.put(93, "blue"); tm.put(26, "violet"); tm.put(63, "red"); tm.put(53, "green"); tm.put(96, "five"); // put values in NavigableMap NavigableMap nm = tm.descendingMap(); System.out.println("TreeMap values using descendingMap() method: "); System.out.println("NavigableMap values: " + nm); } }
Output:
TreeMap values using descendingMap() method:
NavigableMap values: {96=five, 93=blue, 63=red, 53=green, 26=violet}
Also read – Strings in java