Sunday 8 June 2014

TreeMap : pollFirstEntry()

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

import java.util.*;

class TreeMapPollFirstEntry{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  Map.Entry<Integer,String> firstEntry;
  
  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);
  
  firstEntry = myMap.pollFirstEntry(); 
  
  System.out.println("First Entry in myMap is");
  System.out.println(firstEntry); 
  System.out.println("Elements in myMap are");
  System.out.println(myMap);  
 }
}

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



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment