Java subMap(K fromKey,boolean fromInclusive,� K toKey,� boolean toInclusive) method of ConcurrentNavigableMap interface.

Java subMap(K fromKey,boolean fromInclusive,� K toKey,� boolean toInclusive) method of ConcurrentNavigableMap interface.


Posted in : Core Java Posted on : March 17, 2011 at 1:04 PM Comments : [ 0 ]

In this example we will discuss how can submap(k fromKey, boolean fromInclusive, k toKey, boolean toInclusive) method be implemented in

Java subMap(K fromKey,

                        boolean fromInclusive, 

                        K toKey, boolean toInclusive) 

                        method of ConcurrentNavigableMap interface.

In this example we will discuss how can submap(k fromKey, boolean fromInclusive, k toKey, boolean toInclusive) method be implemented in ConcurrentNavigableMap interface in java.

Syntax

ConcurrentNavigableMap<K,V> subMap(K fromKey,
                                                                    boolean fromInclusive,
                                                                    K toKey,
                                                                    boolean toInclusive)

It returns a view portion between the keys range from 'fromKey' to 'toKey' of the underlying map. If there is no mapping exist into the returned map (i.e. map is empty) then 'fromKey' and 'toKey' will be equal and it will be empty unless the 'fromExclusive' and 'toExclusive' both are true. Since the returned map is backed by the underlying map, therefore any change occurs into the returned map is shown into the underlying map, and vice-versa. All optional operations of the underlying map that it supports is supported by the returned map. The returned map throws an IllegalArgumentException if an inserted key belongs to its outside range ,or to construct a submap either of whose endpoints lie outside its range.

Parameter description

fromKey : The starting point of the keys from where you want to view the returned map.
fromInclusive : true if the start point is to be included in the returned view
toKey : The endpoint of the keys till you want to view in the returned map
toInclusive : true if the last point is to be included in the backed view 

Example of subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)

In this example we will show you how does subMap(k fromKey, boolean fromInclusive, k toKey, boolean toInclusive) method work in ConcurrentNavigableMap interface. This example will help you to understand how can you find a submap (including start and end both point of the returned map) from the underlying map.

Exmaple :

package devmanuals.com;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.NavigableSet;
public class CNMSubMapBoolean {
  public static void main(String args[]){
    ConcurrentNavigableMap cnm = 
              new ConcurrentSkipListMap();
    ConcurrentNavigableMap cnm1 = 
              new ConcurrentSkipListMap();
    ConcurrentNavigableMap cnm2 = 
              new ConcurrentSkipListMap();
    ConcurrentNavigableMap cnm3 = 
              new ConcurrentSkipListMap();
    cnm.put("A""Rose");
    cnm.put("B""India");
    cnm.put("C""Network");
    cnm.put("D""Delhi");
    System.out.println("Map = "+cnm);
    NavigableSet ns= cnm.keySet();
    System.out.println("Set of keys of map = "+ns);
    cnm1 = cnm.subMap("B",true , "D"true);
    System.out.println("Submap = "+cnm1);
    NavigableSet ns1= cnm1.keySet();
    System.out.println("Set of keys of submap = "+ns1);
    cnm2 = cnm.subMap("A",true, "D",false);
    System.out.println("Submap = "+cnm2);
    NavigableSet ns2= cnm2.keySet();
    System.out.println("Set of keys = "+ns2);
    cnm3 = cnm.subMap("A", false, "A"false);
    System.out.println("Submap = "+cnm3);
   }
}

Output :

Map = {A=Rose, B=India, C=Network, D=Delhi}

Set of keys of map = [A, B, C, D]

Submap = {B=India, C=Network, D=Delhi}

Set of keys of submap = [B, C, D]

Submap = {A=Rose, B=India, C=Network}

Set of keys = [A, B, C]

Submap = {}

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics