Java headMap(K toKey, boolean inclusive) method of ConcurrentNavigableMap Interface.

Java headMap(K toKey, boolean inclusive) method of ConcurrentNavigableMap Interface.


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

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

Java headMap(K toKey, boolean inclusive) method of ConcurrentNavigableMap Interface.

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

Syntax :

ConcurrentNavigableMap<K,V> headMap(K toKey, boolean inclusive)

This map returns the view part of the underlying map in which the keys are smaller than or equal (if inclusive=true) to the 'toKey'. 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 headMap(toKey, false).

Parameter description

toKey : It is the endpoint of the keys at where do you want to view the map in returned map.

inclusive : It will be true if the last point is included in returned view map.

Example of headMap(k toKey, boolean inclusive) method.

In this example we will discuss how does headMap(k toKey,boolean inclusive) method work in ConcurrentNavigableMap interface. This example will help you to understand how can you obtained a part of top elements of the underlying map upto the defined number ( the number is taken as the parameter by this method).

Example :

package devmanuals.com;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ConcurrentNavigableMap;
public class CNMHeadMapInclusive {
  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(5true);
    System.out.println("Top elements of map upto 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,"H");
    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 upto key '5' = {1=A, 2=B, 3=C, 4=D, 5=E}

Size of map = 5

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

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

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics