Sunday, 8 June 2014

TreeMap : subMap (K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)

public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
K toKey, boolean toInclusive)
Returns a view of the portion of this map whose keys range from fromKey to toKey.

import java.util.*;

class TreeMapSubMap{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  NavigableMap<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, true, 7, false);
  
  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. If fromKey and toKey are equal, the returned map is empty unless fromInclusive and toInclusive are both true.

import java.util.*;

class TreeMapSubMap1{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  NavigableMap<Integer,String> subMap1;
  NavigableMap<Integer,String> subMap2;
  NavigableMap<Integer,String> subMap3;
  NavigableMap<Integer,String> subMap4;
  
  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");
  
  subMap1 = myMap.subMap(2, false, 2, false);
  subMap2 = myMap.subMap(2, false, 2, true);
  subMap3 = myMap.subMap(2, true, 2, false);
  subMap4 = myMap.subMap(2, true, 2, true);
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.println("Elements in subMap1 are");
  System.out.println(subMap1);
  
  System.out.println("Elements in subMap2 are");
  System.out.println(subMap2);
  
  System.out.println("Elements in subMap3 are");
  System.out.println(subMap3);
  
  System.out.println("Elements in subMap4 are");
  System.out.println(subMap4);
 }
}

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

2. 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 TreeMapSubMap2{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  NavigableMap<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, true, 100, false);
  
  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}

3. Throws 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 TreeMapSubMap3{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  NavigableMap<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, true, 100, false);
 }
}

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 TreeMapSubMap3.main(TreeMapSubMap3.java:24)

4. 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 TreeMapSubMap4{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  NavigableMap<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, true, 2, false);
 }
}

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 TreeMapSubMap4.main(TreeMapSubMap4.java:24)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment