Sunday, January 16, 2011

subMap

1- SortedMap subMap(K fromKey, K toKey);
2-   public NavigableMap subMap(K fromKey, boolean fromInclusive,K toKey,   boolean toInclusive) {}
------------------------------------------------------------------------------------------------------------
   1- SortedMap subMap(K fromKey, K toKey);
1-Returns a view of the portion of this map whose keys range from fromKey  inclusive, to toKey , exclusive.
2-If fromKey and toKey are equal, the returned map is empty. 
3-The returned map is backed by this map, so changes
------------------------------------------------------------------------------------------------------------
2-   public NavigableMap subMap(K fromKey, boolean fromInclusive,K toKey,   boolean toInclusive) {}
This method is same as upper method but one main difference is that it provide extra facility to inclusive  and exclusive key as user demand
 ---------------------------------------------------------------------------------------------------------------------------------

Example:

import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;

public class Test {

    public static void main(String[] args) {
        TreeMap   setmap=new TreeMap();
        setmap.put(1, "AAA");
        setmap.put(2, "BBB");
        setmap.put(3, "CCC");
        setmap.put(4, "DDD");
        setmap.put(5, "EEE");
        SortedMap< Integer, String>  simple=setmap.subMap(2, 4);
        System.out.println(simple);
        SortedMap< Integer, String>  simple1=setmap.subMap(2,false, 4,true);
        System.out.println(simple1);
       
       
       
    }
   
}


OUTPUT :
{2=BBB, 3=CCC}
{3=CCC, 4=DDD}