Java tailMap(K fromKey, boolean inclusive) method of ConcurrentNavigableMap interface.

Java tailMap(K fromKey, boolean inclusive) method of ConcurrentNavigableMap interface.


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

In this section we will discuss how can tailMap(k fromKey boolean inclusive) method be implemented in ConcurrententNavigableMap interface in java. ConcurrentNavigableMap tailMap(K fromKey,boolean inclusive) This method gives back the view part of the underlying map in which the keys are larger than or equal (if inclusive=true) to the 'fromKey'.

Java tailMap(K fromKey, boolean inclusive) method of ConcurrentNavigableMap interface.

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

Syntax

ConcurrentNavigableMap<K,V> tailMap(K fromKey,boolean inclusive)

This method gives back the view part of the underlying map in which the keys are larger than or equal (if inclusive=true) 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 point from where do you want to view a part of the underlying map.

inclusive : It will be 'true' if the last point is to be included in the backed view.

Example of tailMap(k fromKey, boolean inclusive) method in java.

In this example we will show you how does tailMap(k fromKey, boolean inclusive) work in ConcurrentNavigableMap interface. This example will help you to understand how can you find the tail mappings of a map whose keys are greater than or equal (if inclusive = true) to 'fromKey'.

Example :

package devmanuals.com;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.NavigableSet;
public class CNMTailMapBoolean {
  public static void main (String args[]){
    ConcurrentNavigableMap<Integer,String> cnm, cnm1;
    NavigableSet<Integer> ns;
    cnm= new ConcurrentSkipListMap<Integer,String>();
    String str,str1,str2,str3;
    str="Rose";
    str1="India";
    str2="Network";
    str3="Delhi";
    cnm.put(1, str);
    cnm.put(2, str1);
    cnm.put(3, str2);
    cnm.put(4, str3);
    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(2,true);
    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 = {1=Rose, 2=India, 3=Network, 4=Delhi}

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

Size of map = 4

Tail map = {2=India, 3=Network, 4=Delhi}

Set of keys of tail map = [2, 3, 4]

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