Java tailMap(k fromKey) method of ConcurrentNavigableMap interface.

Java tailMap(k fromKey) method of ConcurrentNavigableMap interface.


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

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

Java tailMap(k fromKey) method of ConcurrentNavigableMap interface.

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

Syntax

ConcurrentNavigableMap<K,V> tailMap(K fromKey)

This method gives back the view part of the underlying map, in which the keys are existed than the larger or equal to the 'fromKey'. Since the returned map is returned 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 the tailMap(fromKey, true).

Parameter description

fromKey : It is the start point (inclusive) of the keys from where you want to view the returned map.

Example of tailMap(k fromKey)

In this example we will show you how does tailMap(k fromKey) method work in ConcurrentNavigableMap interface. This example will help you to understand how can you obtain a part of the map whose keys are greater than or equal to the 'fromKey'.

Example :

package devmanuals.com;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.NavigableSet;
public class CNMTailMap {
  public static void main (String args[]){
    ConcurrentNavigableMap<String,String> cnm, cnm1;
    NavigableSet<String> ns;
    cnm= new ConcurrentSkipListMap<String,String>();
    cnm.put("A""Rose");
    cnm.put("B""India");
    cnm.put("C""Network");
    cnm.put("D""Delhi");
    System.out.println("Map = "+cnm);
    ns = cnm.keySet();
    System.out.println("Set of keys = "+ns);
    System.out.println("Size of map = "+cnm.size());
    //implementation of the method
    cnm1 = cnm.tailMap("B");
    System.out.println("Tail map = "+cnm1);
    ns= cnm1.keySet();
    System.out.println("Set of keys of tail map = "+ns);
    System.out.println("Size of tail map = "+cnm1.size());
  }
}

Output :

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

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

Size of map = 4

Tail map = {B=India, C=Network, D=Delhi}

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

Size of tail map = 3

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics