Sunday 8 June 2014

TreeMap : tailMap (K fromKey, boolean inclusive)

public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive)
Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.

import java.util.*;

class TreeMapTailMap1{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  NavigableMap<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(5, "mno");
  myMap.put(7, "mnop");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  tailMap = myMap.tailMap(5, true);
  
  System.out.println("Elements in tailMap are");
  System.out.println(tailMap);
 }
}

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


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment