Saturday 7 June 2014

TreeMap : lastEntry()

public Map.Entry<K,V> lastEntry()
Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

import java.util.*;

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

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



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment