Friday 6 June 2014

TreeMap : firstKey() : Get the First Key currently in the map

public K firstKey()
Returns the first (lowest) key currently in this map.

import java.util.*;

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

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




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment