Sunday 8 June 2014

TreeMap : subMap (K fromKey, K toKey)

public SortedMap<K,V> subMap(K fromKey, K toKey)
Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.

import java.util.*;

class TreeMapSubMapEx1{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  SortedMap<Integer,String> subMap;
  
  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");
  
  subMap = myMap.subMap(2, 7);
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.println("Elements in subMap are");
  System.out.println(subMap);
 }
}

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

1. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa.
import java.util.*;

class TreeMapSubMapEx2{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  SortedMap<Integer,String> subMap;
  
  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");
  
  subMap = myMap.subMap(5, 100);
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.println("Elements in subMap are");
  System.out.println(subMap);
  
  System.out.println("Adding Key 55 to subMap");
  subMap.put(55, "s");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.println("Elements in subMap are");
  System.out.println(subMap);
 }
}

Output
Elements in myMap are
{1=m, 2=w, 3=mn, 4=mno, 6=wxy, 7=mnop, 8=wxyz}
Elements in subMap are
{6=wxy, 7=mnop, 8=wxyz}
Adding Key 55 to subMap
Elements in myMap are
{1=m, 2=w, 3=mn, 4=mno, 6=wxy, 7=mnop, 8=wxyz, 55=s}
Elements in subMap are
{6=wxy, 7=mnop, 8=wxyz, 55=s}

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

import java.util.*;

class TreeMapSubMapEx3{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  SortedMap<Integer,String> subMap;
  
  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);
  
  System.out.println("Elements in subMap are");
  subMap = myMap.subMap(null, 100);
 }
}

Output
Elements in myMap are
{1=m, 2=w, 3=mn, 4=mno, 6=wxy, 7=mnop, 8=wxyz}
Elements in subMap are
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1290)
        at java.util.TreeMap$NavigableSubMap.<init>(TreeMap.java:1363)
        at java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:1851)
        at java.util.TreeMap.subMap(TreeMap.java:909)
        at java.util.TreeMap.subMap(TreeMap.java:950)
        at TreeMapSubMapEx3.main(TreeMapSubMapEx3.java:24)

3. Throws IllegalArgumentException - if fromKey is greater than toKey; or if this map itself has a restricted range, and fromKey or toKey lies outside the bounds of the range

import java.util.*;

class TreeMapSubMapEx4{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  SortedMap<Integer,String> subMap;
  
  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);
  
  System.out.println("Elements in subMap are");
  subMap = myMap.subMap(5, 2);
 }
}

Output
Elements in myMap are
{1=m, 2=w, 3=mn, 4=mno, 6=wxy, 7=mnop, 8=wxyz}
Elements in subMap are
Exception in thread "main" java.lang.IllegalArgumentException: fromKey > toKey
        at java.util.TreeMap$NavigableSubMap.<init>(TreeMap.java:1364)
        at java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:1851)
        at java.util.TreeMap.subMap(TreeMap.java:909)
        at java.util.TreeMap.subMap(TreeMap.java:950)
        at TreeMapSubMapEx4.main(TreeMapSubMapEx4.java:24)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment