ConcurrentSkipListMap

java.util.concurrent.ConcurrentSkipListMap Example

ConcurrentSkipListMap is a scalable concurrent navigable map implementation. The map is sorted based on natural ordering of its keys or by comparator depending on the usage of constructor.

ConcurrentSkipListMap  class implements a concurrent  variant of Skip lists which provides expected average log(n) time cost for the get, put, containsKey and remove operations. These methods are multiple thread based  and safe for executing concurrently.

It has method  headmap which returns a view of the map containing the keys which are strictly less than the given key. TailMap method returns a view of the map containing the keys greater than or equal to given key. Submap method returns a view of the original map which contains all keys from including key to excluding key. Concurrent Navigable Map interface has other methods like descendingKeySet, descendingMap and KeySet (navigable).

1. Source Code Example

The example below shows the sample for ConcurrentSkipListMap implementation and usage.

ConcurrentSkipListMap.java:

package com.architectcorner.util.concurrent;

import java.util.Iterator;
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;

/**
 * @author Bhagvan Kommadi
 *This example demonstrates the usage of the ConcurrentSkipListMap 
 *
 */
public class ConcurrentSkipListMapExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ConcurrentSkipListMap<String,String> concurrentSkipListMap = new ConcurrentSkipListMap<String,String>();
        concurrentSkipListMap.put("1111", "Tom Smith");
        concurrentSkipListMap.put("2222","David Jones");
        concurrentSkipListMap.put("3333", "Jim Anderson");
        concurrentSkipListMap.put("4444", "John Abraham");
        concurrentSkipListMap.put("5555", "Brad Pitt");
        
        System.out.println("The name associated with id 1111 is "+ concurrentSkipListMap.get("1111"));
        
        NavigableSet navigableKeySet = concurrentSkipListMap.keySet();
	
	    
	    System.out.println("The keys associated with this map are ");
	    for(Iterator iterator = navigableKeySet.iterator();iterator.hasNext();)
	    {
	    	System.out.println(iterator.next());
	    }
	    
	    ConcurrentNavigableMap<String,String> subMap = concurrentSkipListMap.subMap("1111", "3333");
	    
	    NavigableSet navigableSubKeySet = subMap.keySet();
	    
	    System.out.println("The keys associated with the submap keys from 1111 to 3333 are");
	    
	    for(Iterator subMapIterator = navigableSubKeySet.iterator(); subMapIterator.hasNext();)
	    {
	    	System.out.println(subMapIterator.next());
	    }
	    
	    ConcurrentNavigableMap<String,String> headerMap = concurrentSkipListMap.headMap("2222");
	    
	    System.out.println("The keys associated with the headMap less than 2222");
	    
	    NavigableSet navigableHeadMapKeySet = headerMap.keySet();
	    
	    for(Iterator headMapIterator = navigableHeadMapKeySet.iterator(); headMapIterator.hasNext();)
	    {
	    	System.out.println(headMapIterator.next());
	    }
	    
	    ConcurrentNavigableMap<String,String> tailMap = concurrentSkipListMap.tailMap("1111");
	    
	    System.out.println("Thekeys associated with the tailMap less than 1111");
	    NavigableSet navigableTailMapKeySet = tailMap.keySet();
	    
	    for(Iterator tailMapIterator = navigableTailMapKeySet.iterator(); tailMapIterator.hasNext();)
	    {
	    	System.out.println(tailMapIterator.next());
	    }
	}

}

Output

The name associated with id 1111 is Tom Smith
The keys associated with this map are 
1111
2222
3333
4444
5555
The keys associated with the submap keys from 1111 to 3333 are
1111
2222
The keys associated with the headMap less than 2222
1111
Thekeys associated with the tailMap less than 1111
1111
2222
3333
4444
5555
Tip
ConcurrentSkipListMap  can be used for scalable concurrent navigable maps.

2. Conclusion

ConcurrentSkipListMap is used for storing key and value based data. The concurrentskiplistmap has methods to retrieve the keyset which is navigable and provides methods for headmap, tailmap and submap.

Download
You can download the source code of the example here: ConcurrentSkipListMapExample.zip

Bhagvan Kommadi

Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button