Friday, 6 June 2014

TreeMap : firstEntry() : get the First Entry in TreeMap

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

import java.util.*;

class TreeMapFirstEntry{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  Map.Entry<Integer, String> firstEntry;
  
  myMap = new TreeMap<> ();
  
  /* Add Data to myMap */
  myMap.put(1, "a");
  myMap.put(2, "ab");
  myMap.put(3, "abc");
  myMap.put(4, "abcd");
  
  firstEntry = myMap.firstEntry();
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  System.out.println("First Entry in myMap is");
  System.out.println(firstEntry);
 }
}

Output
Elements in myMap are
{1=a, 2=ab, 3=abc, 4=abcd}
First Entry in myMap is
1=a



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment