Java headMap(K toKey) method of ConcurrentNavigableMap interface.

Java headMap(K toKey) method of ConcurrentNavigableMap interface.


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

In this section we will discuss how can headMap(k toKey) method be implemented in ConcurrentNavigableMap interface in java. ConcurrentNavigableMap headMap(K toKey) This method returns the view part of the underlying map whose keys are stringently less than the 'toKey'.

Java headMap(K toKey) method of ConcurrentNavigableMap interface.

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

Syntax

ConcurrentNavigableMap<K,V> headMap(K toKey)

This method returns the view part of the underlying map whose keys are stringently less than the 'toKey'. 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 the headMap(toKey, false).

Parameter description

toKey : It is the last endpoint (exclusive) of the keys at which you want to find in the returned map.

Example of headMap(k toKey) method

In this example we will discuss how does headMap(k toKey) method work in ConcurrentNavigableMap interface. This example will help you to understand how can you obtain a part of top element from the underlying map (but it displays the elements less than the defined number taken into the parameter by this method).

Example :

package devmanuals.com;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ConcurrentNavigableMap;
public class CNMHeadMap {
  public static void main (String args[]){
    ConcurrentNavigableMap<Integer,String> cnm = 
              new ConcurrentSkipListMap<Integer,String>();
    cnm.put(1,"A");
   cnm.put(2,"B");
   cnm.put(3,"C");
    cnm.put(4,"D");
    cnm.put(5,"E");
    cnm.put(6,"F");
    System.out.println("Mappings = "+cnm);
    System.out.println("Set of keys = "+cnm.keySet());
    System.out.println("First entry of the map : "+cnm.firstEntry());
   System.out.println("Last entry of the map : "+cnm.lastEntry());
    cnm=cnm.headMap(5);
    System.out.println("Top elements of map less than key '5' = "+cnm);
    System.out.println("Size of map = "+cnm.size());
    for(int i=0; i<=6;i++){
      cnm.remove(3);
    }
    System.out.println("Remaining mappings of the map = "+cnm);
    cnm.putIfAbsent(3,"G");
    System.out.println("Mapping after adding new element= "+cnm);
  }
}

Output :

Mappings = {1=A, 2=B, 3=C, 4=D, 5=E, 6=F}

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

First entry of the map : 1=A

Last entry of the map : 6=F

Top elements of map less than key '5' = {1=A, 2=B, 3=C, 4=D}

Size of map = 4

Remaining mappings of the map = {1=A, 2=B, 4=D}

Mapping after adding new element= {1=A, 2=B, 3=G, 4=D}

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics