<file_sep>package concurrent.sets;

import java.util.concurrent.CopyOnWriteArraySet;

public class ConcurrentSkipListSets {

	public static void main(String[] args) {
		CopyOnWriteArraySet<Integer> set = new CopyOnWriteArraySet<>();
		set.add(1);
		set.add(1);
		set.add(null);
		set.add(null);
		
		for (Integer integer : set) {
			System.out.println(integer);
		}
			
		
		
	}
}
<file_sep>package concurrent.maps;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentSkipListMap;

/**
 * It is 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. It provides log(n) time
 * complexity for the following operations :- containsKey, get, put and remove
 * Insertion, removal, update, and access operations safely execute concurrently
 * by multiple threads. The Iterator does not give a guarantee of showing the
 * consistent data. Iterators and Spliterators are weakly consistent. and does not throw
 * ConcurrentModificationException. This class and its views and iterators
 * implement all of the optional methods of the Map and Iterator interfaces.
 * Like most other concurrent collections, this class does not permit the use of
 * null keys or values because some null return values cannot be reliably
 * distinguished from the absence of elements.
 * 
 * Ascending key ordered views and their iterators are faster than descending ones.
 * The reason is that ConcurrentSkipListMap internally uses a skip-list and skip-list are effectively
 * a complicated form of linked list with the links going in the direction of an ordering of the list entries.
 * When we traverse the skip list in the forward direction, we are simply following the links. 
 * Each next() call is an O(1) operation.
 * When we traverse the skip list in the reverse direction, each next() call has to find the key 
 * before the last key returned. This is an O(logN) operation.
 * 
 * @author choudshe
 *
 */
public class ConcurrentSkipListMaps {

	public static void main(String[] args) {
		Map<Integer, String> concurrentSkipListMap = new ConcurrentSkipListMap<>();
		concurrentSkipListMap.put(1, "one");
		concurrentSkipListMap.put(3, "three");
		concurrentSkipListMap.put(2, "two");
		concurrentSkipListMap.put(4, "four");
		concurrentSkipListMap.put(6, "six");
		concurrentSkipListMap.put(5, "five");
		
		Iterator<Integer> itr = concurrentSkipListMap.keySet().iterator();

	}
}
<file_sep>package concurrent.maps;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
/**
 * This example here shows that when we iterate over a concurrent HashMap using an iterator
 * and removes an element from it and if another thread add the same element to it concurrently
 * at the same time then we wont be able to get the exact picture and instead we will see that removal
 * has never happened.
 * According to java specification :-
 * Iterators and Enumerations return elements reflecting the state of the hash table at some point at or 
 * since the creation of the iterator/enumeration. 
 * so here in our example we removed the key 2 from our map using the remove() method of the 
 * iterator and asked the main thread to sleep for 10 seconds and at the same time another thread t
 * adds the key 2 to it. so the iterator prints key 2 in its output.
 * 
 * Output when run in single threaded environment :-
 * 
 * ********iteration by iterator*********
key : 1 value : one
key : 2 value : null
key : 3 value : three
key : 4 value : four
*********final iteration********
key : 1 value : one
key : 3 value : three
key : 4 value : four

Output when run in multithreaded environment :-

********iteration by iterator*********
key : 1 value : one
key : 2 value : two
key : 3 value : three
key : 4 value : four
*********final iteration********
key : 1 value : one
key : 2 value : two
key : 3 value : three
key : 4 value : four

 * @author choudshe
 *
 */
public class ConcurrentHashMaps {

	public static void main(String[] args) {
		ConcurrentHashMap<Integer,String> map = new ConcurrentHashMap<>();
		map.put(1, "one");
		map.put(2, "two");
		map.put(3, "three");
		map.put(4, "four");
		
		Iterator<Integer> itr = map.keySet().iterator();
		Thread t = new Thread(new Runnable() {
			
			@Override
			public void run() {

				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				map.put(2, "two");
			}
		});
		t.start();
		
		
		System.out.println("********iteration by iterator*********");
		while(itr.hasNext()) {
			int i = itr.next();
			if(i==2) {
			itr.remove();
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
			System.out.println("key : "+i+" value : "+map.get(i));
		}
		System.out.println("*********final iteration********");
		for (Integer x : map.keySet()) {
			System.out.println("key : "+x+" value : "+map.get(x));
		}
	}
}
