set

In software, we rarely have meaningful requirements. Even if we do, the only measure of success that matters is whether our solution solves the customerís shifting idea of what their problem is. Jeff Atwood

ConcurrentSkipListSet

Language Java | Level Intermediate | Category Java collections framework (JCF) | 2015-07-24 11:01:09

0 Comments


Definition

A scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap. The elements of the set are kept sorted according to their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

Character

Sets are collections that contain no duplicates.
Implementation based on a ConcurrentSkipListMap.
Sorted by natural ordering or comparator provided in creation time.
Do not throw concurrent modification exception.

source Code

								
package com.careerdrill.collections.set;

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

public class ConcurrentSkipListSet {

	public static void main(String[] args) {
		ConcurrentSkipListMap< String, String > concurrentSkipListMap = new ConcurrentSkipListMap< String, String >();
        concurrentSkipListMap.put("1972", "C");
        concurrentSkipListMap.put("1983","CPP");
        concurrentSkipListMap.put("1987", "Perl");
        concurrentSkipListMap.put("1991", "Python");
        concurrentSkipListMap.put("2009", "Node JS");
        
        System.out.println("The name associated with id 1972 is "+ concurrentSkipListMap.get("1972"));
        
        NavigableSet< String > navigableKeySet = concurrentSkipListMap.keySet();
	
	    
	    System.out.print("The keys associated with this map are : ");
	    
	    for(Iterator< String > iterator = navigableKeySet.iterator();iterator.hasNext();) {
	    	System.out.print(iterator.next() + " ");
	    }
	    
	    ConcurrentNavigableMap< String, String > subMap = concurrentSkipListMap.subMap("1987", "2009");
	    
	    NavigableSet< String > navigableSubKeySet = subMap.keySet();
	    
	    System.out.print("\nThe keys associated with the sub map keys from 1987 to 2009 are : ");
	    
	    for(Iterator< String > subMapIterator = navigableSubKeySet.iterator(); subMapIterator.hasNext();) {
	    	System.out.print(subMapIterator.next() + " ");
	    }
	    
	    ConcurrentNavigableMap< String, String > headerMap = concurrentSkipListMap.headMap("1987");
	    
	    System.out.print("\nThe keys associated with the head map less than 1987 : ");
	    
	    NavigableSet< String > navigableHeadMapKeySet = headerMap.keySet();
	    
	    for(Iterator < String > headMapIterator = navigableHeadMapKeySet.iterator(); headMapIterator.hasNext();)  {
	    	System.out.print(headMapIterator.next() + " ");
	    }
	    
	    ConcurrentNavigableMap< String, String > tailMap = concurrentSkipListMap.tailMap("1972");
	    
	    System.out.print("\nThe keys associated with the tail map greater than 1972 : ");
	    NavigableSet< String > navigableTailMapKeySet = tailMap.keySet();
	    
	    for(Iterator< String > tailMapIterator = navigableTailMapKeySet.iterator(); tailMapIterator.hasNext();) {
	    	System.out.print(tailMapIterator.next() + " ");
	    }

	}

}
						

Output

    					 
The name associated with id 1972 is C
The keys associated with this map are : 1972 1983 1987 1991 2009 
The keys associated with the sub map keys from 1987 to 2009 are : 1987 1991 
The keys associated with the head Map less than 1987 : 1972 1983 
The keys associated with the tail Map greater than 1972 : 1972 1983 1987 1991 2009
    					 
					    

Comparison

CopyOnWriteArraySet ConcurrentSkipListSet
Uses CopyOnWriteArrayList for all of its operations. Implementation based on a ConcurrentSkipListMap.
Thread-safe. Execute concurrently by multiple threads.
Do not throw ConcurrentModificationException. Do not throw ConcurrentModificationException.
Traversal via iterators is fast and cannot encounter interference from other threads. Returns an iterator over the elements in this set in ascending order.

When to use

When you need a sorted container that will be accessed by multiple threads, use ConcurrentSkipListSet.

0 Comments



Please login to add comments.