Saturday 7 June 2014

TreeMap : lastKey()

public K lastKey()
Returns the last (highest) key currently in this map.

import java.util.*;

class TreeMapLastKey{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  Integer lastKey;
  
  myMap = new TreeMap<> ();
  
  /* Add Elements to myMap */
  myMap.put(2, "w");
  myMap.put(4, "wx");
  myMap.put(6, "wxy");
  myMap.put(8, "wxyz");
  
  lastKey = myMap.lastKey();
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.println("Last key of myMap is");
  System.out.println(lastKey);  
 }
}

Output
Elements in myMap are
{2=w, 4=wx, 6=wxy, 8=wxyz}
Last key of myMap is
8



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment