Java Collections Framework - ConcurrentSkipListMap class

Java Collections Framework - ConcurrentSkipListMap class


Posted in : Core Java Posted on : May 25, 2011 at 10:42 AM Comments : [ 0 ]

ConcurrentSkipListMap class extends the abstract class AbstractMap and is implement the ConcurrentNavigableMap interface. ConcurrentSkipListMap is a part of Java collection framework and is available in java.util package.

Java collection framework - ConcurrentSkipListMap

ConcurrentSkipListMap class extends the abstract class AbstractMap and is implement the ConcurrentNavigableMap interface. ConcurrentSkipListMap is a part of Java collection framework and is available in java.util package. The elements of this map are arranged in natural order according to their keys or by a comparator put up at the time of creation. This class doesn't allow 'null' key's or values because the return of few 'null' values can't be reliably differentiated from the absence of elements.

Syntax

public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> implements ConcurrentNavigableMap<K,V>

Constructor of ConcurrentSkipListMap

This class provides constructor for constructing new map according to requirement :

ConcurrentSkipListMap() : It creates a new vacate map into which mappings are sorted according to the keys natural order.

ConcurrentSkipListMap(Comparator<? super K> comparator)  : It creates a new vacate map into which the mappings are sorted according to the defined comparator.

ConcurrentSkipListMap(Map<? extends K,? extends V> m) : It creates a new map that contains the same mappings of a given map into which mappings are sorted according to the keys natural order.

ConcurrentSkipListMap(SortedMap<K,? extends V> m) : It creates a new map that contains the same mappings and follows the order like the defined sorted map.

Methods of ConcurrentSkipListMap

This class provides various methods some of them are :

  1. put() 
  2.                         syntax : public V put(K key,V value)

  3. ceilingKey() 
  4.                         syntax : public K ceilingKey(K key)

  5. floorKey()
  6.                         syntax : public K floorKey(K key)

  7. lastKey()
  8.                         syntax : public K lastKey()

  9. lowerKey(K key)
  10.                         syntax : public K lowerKey(K key)

Example

package devmanuals.com;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class ConcurrentSkipListMapDemo {
  public static void main(String[] args) {
  ConcurrentSkipListMap clm=new ConcurrentSkipListMap();
      clm.put(1,"Sunday");
      clm.put(2,"Monday");
      clm.put(3,"Tuesday");
      clm.put(4,"Wednesday");
      clm.put(5,"Thursday");
      clm.put(6,"Friday");
      clm.put(7,"Saturday");
      System.out.println("Mappings =");
      System.out.println(clm);
      Map.Entry me = clm.lowerEntry(5);
      System.out.println(me);
      System.out.println("Key " + me.getKey());
      System.out.println("Value "+me.getValue());
      SortedMap sm = clm.tailMap(3);
      Set s = sm.keySet();
      System.out.println("Tail elements are");
      System.out.println(clm.tailMap(4));
    }
}

Output :

Mappings =

{1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday}

4=Wednesday

Key =4

Value =Wednesday

Tail elements are

{4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday}

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics