Map

When debugging, novices insert corrective code; experts remove defective code. Richard Pattis

ConcurrentSkipListMap

Language Java | Level Intermediate | Category Java collections framework (JCF) | 2015-07-22 20:41:14

0 Comments


Definition

ConcurrentSkipListMap is a scalable concurrent class which uses concurrent variant of SkipLists. It does not throw ConcurrentModificationException and support execute the operations safely from multiple threads. ConcurrentSkipListMap guarantees average O(log(n)) performance on a wide variety of operations.

Character

Elements are fetched in natural order
Implements a concurrent variant of SkipLists. IT allows to access or update the items concurrently.
The Map.Entry reads from multiple threads and does not support update the value.
Does not permit the use of null keys or values.
Iterator that will never throw ConcurrentModificationException.

source Code

								
package com.careerdrill.collections.map;

import java.util.Map;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;

public class ConcurrentSkipListMapUse {

	public static void main(String[] args) {
		System.out.println("Insert elements to ConcurrentSkipListMap");
		 ConcurrentNavigableMap< String,String > concurrentSkipListMap = new ConcurrentSkipListMap< String,String >();
	        concurrentSkipListMap.put("3", "Java");
	        concurrentSkipListMap.put("2", "CPP");
	        concurrentSkipListMap.put("1", "C");
	        concurrentSkipListMap.put("5", "NodeJS");
	        concurrentSkipListMap.put("4", "CSharp");

	        System.out.println("Get the ceiling Entry of '2' : " + concurrentSkipListMap.ceilingEntry("2"));

	        ConcurrentNavigableMap< String, String > map = concurrentSkipListMap.descendingMap();
			for (Map.Entry< String, String > entry : map.entrySet()){
			    System.out.println(entry.getKey() + "/" + entry.getValue());
			}

	        
	        System.out.println("First Entry from ConcurrentSkipListMap: " + concurrentSkipListMap.firstEntry());
	        System.out.println("Last Entry  from ConcurrentSkipListMap: " + concurrentSkipListMap.lastEntry());
	        System.out.println("Poll Last Entry  from ConcurrentSkipListMap: " + concurrentSkipListMap.pollLastEntry());
	        System.out.println("Last Entry  from ConcurrentSkipListMap: " + concurrentSkipListMap.lastEntry());

	}

}
						

Output

    					 
Insert elements to ConcurrentSkipListMap
Get the ceiling Entry of '2' : 2=CPP
5/NodeJS
4/CSharp
3/Java
2/CPP
1/C
First Entry from ConcurrentSkipListMap: 1=C
Last Entry  from ConcurrentSkipListMap: 5=NodeJS
Poll Last Entry  from ConcurrentSkipListMap: 5=NodeJS
Last Entry  from ConcurrentSkipListMap: 4=CSharp
    					 
					    

Comparison

ConcurrentHashMap ConcurrentSkipListMap
Does not guarantee the runtime of its operations Guarantees average O(log(n)) performance
Does not allow null to be used as a key or value Null value and null keys are not allowed
Do not maintain insertion order Elements fetched in natural order
Support Only Serializable Support cloneable and Serializable
Do not support ceilingEntry, ceilingKey, firstEntry, floorEntry operations Support ceilingEntry, ceilingKey, firstEntry, floorEntry operations

When to use

If you need faster in-order traversal, and can afford the extra cost for insertion, use the SkipListMap.

0 Comments



Please login to add comments.