Friday 6 June 2014

TreeMap : descendingMap() : Get the Descending Map

public NavigableMap<K, V> descendingMap()
Returns a reverse order view of the mappings contained in this map.

import java.util.*;

class TreeMapDescendingMap{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  NavigableMap<Integer, String> navMap;
  
  myMap = new TreeMap<> ();
  
  /* Add Data to myMap */
  myMap.put(1, "a");
  myMap.put(2, "ab");
  myMap.put(3, "abc");
  myMap.put(4, "abcd");
  
  navMap = myMap.descendingMap();
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  System.out.println("Elements in navMap are");
  System.out.println(navMap);
 }
}

Output
Elements in myMap are
{1=a, 2=ab, 3=abc, 4=abcd}
Elements in navMap are
{4=abcd, 3=abc, 2=ab, 1=a}




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment