Sunday 8 June 2014

TreeMap : pollLastEntry()

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

import java.util.*;

class TreeMapPollLastEntry{
 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");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  lastEntry = myMap.pollLastEntry(); 
  
  System.out.println("Last Entry in myMap is");
  System.out.println(lastEntry); 
  System.out.println("Elements in myMap are");
  System.out.println(myMap);  
 }
}

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



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment