Friday 6 June 2014

TreeMap : descendingKeySet : Get the Reverse order set view of keys

public NavigableSet<K> descendingKeySet()
Returns a reverse order NavigableSet view of the keys contained in this map.

import java.util.*;

class TreeMapDescendingKeySet{
  public static void main(String args[]){
    TreeMap<Integer, String> myMap;
  NavigableSet<Integer> mySet;
  
  myMap = new TreeMap<> ();
  
  /* Add data to the myMap */
  myMap.put(1, "First");
  myMap.put(2, "Second");
  myMap.put(3, "Third");
  myMap.put(4, "Fourth");
  
  System.out.println("Data in myMap is");
  System.out.println(myMap);
  
  mySet = myMap.descendingKeySet();
  
  System.out.println("Elements in mySet is");
  System.out.println(mySet);
  
  }
}

Output
Data in myMap is
{1=First, 2=Second, 3=Third, 4=Fourth}
Elements in mySet is
[4, 3, 2, 1]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment