The descendingMap() method is used to return a reverse order view of the mappings contained in this map. The reverse order or descending order of mappings are according to the descending order of keys. The descending map is backed by this map, so changes to the map are reflected in the descending map, and vice-versa.
Declaration Syntax:
public NavigableMap<K,V> descendingMap()
- K : It is the type of keys maintained by this map.
- V : It is the type of mapped values.
Parameters: Not Available
Return Value: A reverse order or descending order view of this map.
Example 1:
Java
// Java program to demonstrate descendingMap() method import java.util.*; public class Example1 { public static void main(String[] args) { // Declaring the tree map of Integer and String TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); // Add the mappings to the tree map using put() treemap.put( 2 , "Two" ); treemap.put( 16 , "Sixteen" ); treemap.put( 8 , "Eight" ); treemap.put( 6 , "Six" ); treemap.put( 10 , "Ten" ); // store the descending order of mappings in dmap NavigableMap dmap = treemap.descendingMap(); System.out.println( "Reverse navigable map values: " + dmap); } } |
Reverse navigable map values: {16=Sixteen, 10=Ten, 8=Eight, 6=Six, 2=Two}
Example 2 :
Java
// Java Program to demonstrate descendingMap() method import java.util.*; public class Example2 { public static void main(String[] args) { // Declaring the tree map of Integer and String TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); // Add the mappings to the tree map using put() treemap.put( 11 , "Abhishek Rout" ); treemap.put( 9 , "Akash Salvi" ); treemap.put( 2 , "Hemant Koul" ); treemap.put( 8 , "Vaibhav Kamble" ); treemap.put( 6 , "Sagar Joshi" ); treemap.put( 10 , "Onkar Dherange" ); treemap.put( 7 , "Rajwardhan Shinde" ); treemap.put( 1 , "Rahul Gavhane" ); treemap.put( 4 , "Abhishek Gadge" ); treemap.put( 3 , "Pratik Kulkarni" ); treemap.put( 5 , "Raviraj Bugge" ); // store the descending order of mappings in dmap NavigableMap dmap = treemap.descendingMap(); // print the mappings System.out.println( "List of students in reverse order: " + dmap); } } |
List of students in reverse order: {11=Abhishek Rout, 10=Onkar Dherange, 9=Akash Salvi, 8=Vaibhav Kamble, 7=Rajwardhan Shinde, 6=Sagar Joshi, 5=Raviraj Bugge, 4=Abhishek Gadge, 3=Pratik Kulkarni, 2=Hemant Koul, 1=Rahul Gavhane}