Sunday 8 June 2014

TreeMap : tailMap (K fromKey)

public SortedMap<K,V> tailMap(K fromKey)
Returns a view of the portion of this map whose keys are greater than or equal to fromKey.

import java.util.*;

class TreeMapTailMap{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  SortedMap<Integer,String> tailMap;
  
  myMap = new TreeMap<> ();
  
  /* Add Elements to myMap */
  myMap.put(2, "w");
  myMap.put(4, "wx");
  myMap.put(6, "wxy");
  myMap.put(8, "wxyz");
  myMap.put(1, "m");
  myMap.put(3, "mn");
  myMap.put(4, "mno");
  myMap.put(7, "mnop");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  tailMap = myMap.tailMap(5);
  
  System.out.println("Elements in tailMap are");
  System.out.println(tailMap);
 }
}

Output
Elements in myMap are
{1=m, 2=w, 3=mn, 4=mno, 6=wxy, 7=mnop, 8=wxyz}
Elements in tailMap are
{6=wxy, 7=mnop, 8=wxyz}

1. Throws NullPointerException if fromKey is null and this map uses natural ordering, or its comparator does not permit null keys.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment