Java Program to Implement ConcurrentSkipListMap API

This Java program is to Implement ConcurrentSkipListMap API. A scalable concurrent ConcurrentNavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
This class implements a concurrent variant of SkipLists providing expected average log(n) time cost for the containsKey, get, put and remove operations and their variants. Insertion, removal, update, and access operations safely execute concurrently by multiple threads.

Here is the source code of the Java program to Implement ConcurrentSkipListMap API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

importjava.util.Collection;

importjava.util.Comparator;

importjava.util.HashMap;

importjava.util.Iterator;

importjava.util.Map;

importjava.util.Map.Entry;

importjava.util.NavigableSet;

importjava.util.Set;

importjava.util.SortedMap;

importjava.util.concurrent.ConcurrentNavigableMap;

importjava.util.concurrent.ConcurrentSkipListMap;

publicclass ConcurrentSkipListMapImpl<K, V>

{

private ConcurrentSkipListMap<K, V> concurrentSkipListMap;

/**

* Constructs a new, empty map, sorted according to the natural ordering of

* the keys.

**/

public ConcurrentSkipListMapImpl()

{

concurrentSkipListMap =new ConcurrentSkipListMap<K, V>();

}

/**

* Constructs a new, empty map, sorted according to the specified

* comparator.

**/

public ConcurrentSkipListMapImpl(Comparator<?super K> comparator)

{

concurrentSkipListMap =new ConcurrentSkipListMap<K, V>(comparator);

}

/**

* Constructs a new map containing the same mappings as the given map,

* sorted according to the natural ordering of the keys.

**/

public ConcurrentSkipListMapImpl(Map<?extends K, ?extends V> m)

{

concurrentSkipListMap =new ConcurrentSkipListMap<K, V>(m);

}

/**

* Constructs a new map containing the same mappings and using the same

* ordering as the specified sorted map.

**/

public ConcurrentSkipListMapImpl(SortedMap<K, ?extends V> m)

{

concurrentSkipListMap =new ConcurrentSkipListMap<K, V>(m);

}

/**

* Returns a key-value mapping associated with the least key greater than or

* equal to the given key, or null if there is no such key.

**/

publicMap.Entry<K, V> ceilingEntry(K key)

{

return concurrentSkipListMap.ceilingEntry(key);

}

/**

* Returns the least key greater than or equal to the given key, or null if

* there is no such key.

**/

public K ceilingKey(K key)

{

return concurrentSkipListMap.ceilingKey(key);

}

/** Removes all of the mappings from this map. **/

publicvoid clear()

{

concurrentSkipListMap.clear();

}

/** Returns a shallow copy of this ConcurrentSkipListMap instance. **/

public ConcurrentSkipListMap<K, V> clone()

{

return concurrentSkipListMap.clone();

}

/**

* Returns the comparator used to order the keys in this map, or null if

* this map uses the natural ordering of its keys.

**/

public Comparator<?super K> comparator()

{

return concurrentSkipListMap.comparator();

}

/** Returns true if this map contains a mapping for the specified key. **/

publicboolean containsKey(Object key)

{

return concurrentSkipListMap.containsKey(key);

}

/** Returns true if this map maps one or more keys to the specified value. **/

publicboolean containsValue(Object value)

{

return concurrentSkipListMap.containsValue(value);

}

/** Returns a reverse order NavigableSet view of the keys contained in this map. **/

public NavigableSet<K> descendingKeySet()

{

return concurrentSkipListMap.descendingKeySet();

}

/** Returns a reverse order view of the mappings contained in this map. **/

public ConcurrentNavigableMap<K, V> descendingMap()

{

return concurrentSkipListMap.descendingMap();

}

/** Returns a Set view of the mappings contained in this map. **/

public Set<Map.Entry<K, V>> entrySet()

{

return concurrentSkipListMap.entrySet();

}

/**

* Returns a key-value mapping associated with the least key in this map, or

* null if the map is empty.

**/

publicMap.Entry<K, V> firstEntry()

{

return concurrentSkipListMap.firstEntry();

}

/** Returns the first (lowest) key currently in this map. **/

public K firstKey()

{

return concurrentSkipListMap.firstKey();

}

/**

* Returns the greatest key less than or equal to the given key, or null if

* there is no such key.

**/

public K floorKey(K key)

{

return concurrentSkipListMap.floorKey(key);

}

/**

* Returns the value to which the specified key is mapped, or null if this

* map contains no mapping for the key.

**/

public V get(Object key)

{

return concurrentSkipListMap.get(key);

}

/**

* Returns a view of the portion of this map whose keys are strictly less

* than toKey.

**/

public ConcurrentNavigableMap<K, V> headMap(K toKey)

{

return concurrentSkipListMap.headMap(toKey);

}

/**

* Returns a view of the portion of this map whose keys are less than (or

System.out.println("the size of the concurrentSkipListMap is "+ concurrentSkipListMap.size());

concurrentSkipListMap.clear();

}

}

advertisements

$ javac ConcurrentSkipListMapImpl.java
$ java ConcurrentSkipListMapImpl
the key set of the concurrentSkipListMap map is
102334458990
the values of the concurrentSkipListMap is
10009345-8923
poll first entry of the map
key = 10 value = 100
poll last entry of the map
key = 90 value = 23
the entry set of the concurrentSkipListMap is
23=034=945=34589=-89
the concurrentSkipListMap contains Key 34 :true
the concurrentSkipListMap contains Value 600 :false
the size of the concurrentSkipListMap is 4

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

If you wish to look at all Java Programming examples, go to Java Programs.

If you liked this Java Program, kindly share, recommend or like below!