bb.util
Class ConcurrentHashSet<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<E>
          extended by bb.util.ConcurrentHashSet<E>
Type Parameters:
E - Type of element stored in the set
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Set<E>

public class ConcurrentHashSet<E>
extends AbstractSet<E>
implements Serializable

Set implementation backed by a concurrent hash table (a ConcurrentHashMap instance). This class is to be used precisely in those situations where CopyOnWriteArraySet has issues:

  1. the set size is large
  2. mutative operations are as common as read-only operations
  3. Iterators must support the mutative remove operation

Because it uses a backing ConcurrentHashMap instance, this class does not permit the null element.

This class offers constant time performance for the basic operations (add, remove, contains and size), assuming that the hash function disperses the elements properly among the buckets.

Iterating over this set requires time proportional to the sum of the ConcurrentHashSet instance's size (the number of elements) plus the "capacity" of the backing ConcurrentHashMap instance (the number of buckets). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important. Note that this class makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

This class is thread safe, and offers the same full concurrency of retrievals, adjustable expected concurrency for updates, and iterator behavior as ConcurrentHashMap.

Author:
Brent Boyer
See Also:
Serialized Form

Field Summary
private  ConcurrentHashMap<E,Object> map
           
private static Object present
          Dummy value to associate with an Object in the backing Map.
(package private) static long serialVersionUID
           
 
Constructor Summary
ConcurrentHashSet()
          Constructs a new empty set.
ConcurrentHashSet(Collection<? extends E> c)
          Constructs a new set containing the elements in the specified collection.
ConcurrentHashSet(int initialCapacity)
          Constructs a new empty set.
ConcurrentHashSet(int initialCapacity, float loadFactor, int concurrencyLevel)
          Constructs a new, empty set.
 
Method Summary
 boolean add(E o)
           
 void clear()
           
 boolean contains(Object o)
           
 boolean isEmpty()
           
 Iterator<E> iterator()
          Returns an Iterator over the elements in this set.
 boolean remove(Object o)
           
 int size()
           
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, containsAll, retainAll, toArray, toArray
 

Field Detail

serialVersionUID

static final long serialVersionUID
See Also:
Constant Field Values

present

private static final Object present
Dummy value to associate with an Object in the backing Map.


map

private final ConcurrentHashMap<E,Object> map
Constructor Detail

ConcurrentHashSet

public ConcurrentHashSet()
Constructs a new empty set. The backing ConcurrentHashMap instance has the default initial capacity (16), load factor (0.75f), and concurrencyLevel (16).


ConcurrentHashSet

public ConcurrentHashSet(int initialCapacity)
                  throws IllegalArgumentException
Constructs a new empty set. The backing ConcurrentHashMap instance has the specified initial capacity, along with the default load factor (0.75f) and concurrencyLevel (16).

Parameters:
initialCapacity - the initial capacity of the hash table
Throws:
IllegalArgumentException - if initialCapacity < 0

ConcurrentHashSet

public ConcurrentHashSet(int initialCapacity,
                         float loadFactor,
                         int concurrencyLevel)
                  throws IllegalArgumentException
Constructs a new, empty set. The backing ConcurrentHashMap instance has the specified initial capacity, load factor, and concurrency level.

Parameters:
initialCapacity - the initial capacity of the hash map
loadFactor - the load factor of the hash map
concurrencyLevel - the concurrency level of the hash map
Throws:
IllegalArgumentException - if initialCapacity < 0; loadFactor <= 0; concurrencyLevel <= 0

ConcurrentHashSet

public ConcurrentHashSet(Collection<? extends E> c)
                  throws IllegalArgumentException
Constructs a new set containing the elements in the specified collection. The backing ConcurrentHashMap instance has an initial capacity sufficient to contain the elements in the specified collection or 16, if that is greater. It also has the default load factor (0.75f) and concurrencyLevel (16).

Parameters:
c - the collection whose elements are to be placed into this set
Throws:
IllegalArgumentException - if c == null
Method Detail

size

public int size()
Specified by:
size in interface Collection<E>
Specified by:
size in interface Set<E>
Specified by:
size in class AbstractCollection<E>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface Set<E>
Overrides:
isEmpty in class AbstractCollection<E>

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection<E>
Specified by:
contains in interface Set<E>
Overrides:
contains in class AbstractCollection<E>

add

public boolean add(E o)
Specified by:
add in interface Collection<E>
Specified by:
add in interface Set<E>
Overrides:
add in class AbstractCollection<E>

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<E>
Specified by:
remove in interface Set<E>
Overrides:
remove in class AbstractCollection<E>

clear

public void clear()
Specified by:
clear in interface Collection<E>
Specified by:
clear in interface Set<E>
Overrides:
clear in class AbstractCollection<E>

iterator

public Iterator<E> iterator()
Returns an Iterator over the elements in this set. The elements are returned in no particular order. The result supports Iterator.remove.

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface Set<E>
Specified by:
iterator in class AbstractCollection<E>
Returns:
an Iterator over the elements in this set