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
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.
You can download the source code of the example here: ConcurrentSkipListMapExample.zip