Java subMap(k fromKey, k toKey) method of ConcurrentNavigableMap interface.

Java subMap(k fromKey, k toKey) method of ConcurrentNavigableMap interface.


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

In this section we will discuss how can subMap(k fromKey, k toKey) method be implemented in ConcurrentNavigableMap interface in java. ConcurrentNavigableMap subMap(K fromKey, K toKey) This method gives back a view portion between the keys range from 'fromKey', 'inclusive', to 'toKey', 'exclusive'.

Java subMap(k fromKey, k toKey) method of ConcurrentNavigableMap interface.

In this section we will discuss how can subMap(k fromKey, k toKey) method be implemented in ConcurrentNavigableMap interface in java.

Syntax

ConcurrentNavigableMap<K,V> subMap(K fromKey, K toKey)

This method gives back a view portion between the keys range from 'fromKey', 'inclusive', to 'toKey', 'exclusive'. The returned map would be empty if the range between 'fromKey' and 'toKey' is equal. 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. This method is equivalent to subMap(fromKey, true, toKey, false). 

Parameter description

fromKey : The starting point of the keys from where you want to view the returned map.

toKey : The endpoint of the keys till you want to view in the returned map.

Example of subMap(k fromKey, k toKey) method.

In this example we will show you how does subMap(k fromKey, k toKey) method work in ConcurrentNavigableMap interface. This example will help you to understand how can you find a submap (including the start point but exclude the end 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 CNMSubMap {
  public static void main(String args[]){
    ConcurrentNavigableMap<Integer,String> cnm,cnm1,cnm2; 
    NavigableSet<Integer> ns,ns1,ns2;
    cnm = new ConcurrentSkipListMap<Integer, String>();
    cnm.put(1,"Rose");
    cnm.put(2,"India");
    cnm.put(3,"Network");
    System.out.println("Map = "+cnm);
    ns= cnm.keySet();
      System.out.println("Set of keys of map = "+ns);
     cnm1 = cnm.subMap(13);
      System.out.println("Submap = "+cnm1);
    ns1= cnm1.keySet();
      System.out.println("Set of keys of submap = "+ns1);
      cnm2 = cnm.subMap(1,true, 3,false);
      System.out.println("Submap = "+cnm2);
    ns2= cnm2.keySet();
      System.out.println("Set of keys = "+ns2);
  }
}

Output :

Map = {1=Rose, 2=India, 3=Network}

Set of keys of map = [1, 2, 3]

Submap = {1=Rose, 2=India}

Set of keys of submap = [1, 2]

Submap = {1=Rose, 2=India}

Set of keys = [1, 2]

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics