fabric.common.util
Class LongHashSet

java.lang.Object
  extended by fabric.common.util.AbstractLongCollection
      extended by fabric.common.util.AbstractLongSet
          extended by fabric.common.util.LongHashSet
All Implemented Interfaces:
LongCollection, LongIterable, LongSet

public class LongHashSet
extends AbstractLongSet
implements LongSet

This class provides a HashMap-backed implementation of the Set interface.

Most operations are O(1), assuming no hash collisions. In the worst case (where all hashes collide), operations are O(n). Setting the initial capacity too low will force many resizing operations, but setting the initial capacity too high (or loadfactor too low) leads to wasted memory and slower iteration.

HashSet accepts the null key and null values. It is not synchronized, so if you need multi-threaded access, consider using:
Set s = Collections.synchronizedSet(new HashSet(...));

The iterators are fail-fast, meaning that any structural modification, except for remove() called on the iterator itself, cause the iterator to throw a ConcurrentModificationException rather than exhibit non-deterministic behavior.

Since:
1.2
Author:
Jon Zeppieri, Eric Blake (ebb9@email.byu.edu)
See Also:
LongCollection, LongSet, LongKeyHashMap

Constructor Summary
LongHashSet()
          Construct a new, empty HashSet whose backing HashMap has the default capacity (11) and loadFacor (0.75).
LongHashSet(int initialCapacity)
          Construct a new, empty HashSet whose backing HashMap has the supplied capacity and the default load factor (0.75).
LongHashSet(int initialCapacity, float loadFactor)
          Construct a new, empty HashSet whose backing HashMap has the supplied capacity and load factor.
LongHashSet(LongCollection c)
          Construct a new HashSet with the same elements as are in the supplied collection (eliminating any duplicates, of course).
 
Method Summary
 boolean add(long v)
          Adds the given Object to the set if it is not already in the Set.
 void clear()
          Empties this Set of all elements; this takes constant time.
 boolean contains(long v)
          Returns true if the supplied element is in this Set.
 boolean isEmpty()
          Returns true if this set has no elements in it.
 LongIterator iterator()
          Returns an Iterator over the elements of this Set, which visits the elements in no particular order.
 boolean remove(long v)
          Removes the supplied Object from this Set if it is in the Set.
 int size()
          Returns the number of elements in this Set (its cardinality).
 
Methods inherited from class fabric.common.util.AbstractLongSet
equals, hashCode, removeAll
 
Methods inherited from class fabric.common.util.AbstractLongCollection
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 fabric.common.util.LongSet
addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray
 

Constructor Detail

LongHashSet

public LongHashSet()
Construct a new, empty HashSet whose backing HashMap has the default capacity (11) and loadFacor (0.75).


LongHashSet

public LongHashSet(int initialCapacity)
Construct a new, empty HashSet whose backing HashMap has the supplied capacity and the default load factor (0.75).

Parameters:
initialCapacity - the initial capacity of the backing HashMap
Throws:
java.lang.IllegalArgumentException - if the capacity is negative

LongHashSet

public LongHashSet(int initialCapacity,
                   float loadFactor)
Construct a new, empty HashSet whose backing HashMap has the supplied capacity and load factor.

Parameters:
initialCapacity - the initial capacity of the backing HashMap
loadFactor - the load factor of the backing HashMap
Throws:
java.lang.IllegalArgumentException - if either argument is negative, or if loadFactor is POSITIVE_INFINITY or NaN

LongHashSet

public LongHashSet(LongCollection c)
Construct a new HashSet with the same elements as are in the supplied collection (eliminating any duplicates, of course). The backing storage has twice the size of the collection, or the default size of 11, whichever is greater; and the default load factor (0.75).

Parameters:
c - a collection of initial set elements
Throws:
java.lang.NullPointerException - if c is null
Method Detail

add

public boolean add(long v)
Adds the given Object to the set if it is not already in the Set. This set permits a null element.

Specified by:
add in interface LongCollection
Specified by:
add in interface LongSet
Overrides:
add in class AbstractLongCollection
Parameters:
v - the Object to add to this Set
Returns:
true if the set did not already contain v

clear

public void clear()
Empties this Set of all elements; this takes constant time.

Specified by:
clear in interface LongCollection
Specified by:
clear in interface LongSet
Overrides:
clear in class AbstractLongCollection
See Also:
LongIterator.remove()

contains

public boolean contains(long v)
Returns true if the supplied element is in this Set.

Specified by:
contains in interface LongCollection
Specified by:
contains in interface LongSet
Overrides:
contains in class AbstractLongCollection
Parameters:
v - the long to look for
Returns:
true if v is in the set

isEmpty

public boolean isEmpty()
Returns true if this set has no elements in it.

Specified by:
isEmpty in interface LongCollection
Specified by:
isEmpty in interface LongSet
Overrides:
isEmpty in class AbstractLongCollection
Returns:
size() == 0.
See Also:
AbstractLongCollection.size()

iterator

public LongIterator iterator()
Returns an Iterator over the elements of this Set, which visits the elements in no particular order. For this class, the Iterator allows removal of elements. The iterator is fail-fast, and will throw a ConcurrentModificationException if the set is modified externally.

Specified by:
iterator in interface LongCollection
Specified by:
iterator in interface LongIterable
Specified by:
iterator in interface LongSet
Specified by:
iterator in class AbstractLongCollection
Returns:
a set iterator

remove

public boolean remove(long v)
Removes the supplied Object from this Set if it is in the Set.

Specified by:
remove in interface LongCollection
Specified by:
remove in interface LongSet
Overrides:
remove in class AbstractLongCollection
Parameters:
v - the long to remove
Returns:
true if an element was removed
See Also:
LongIterator.remove()

size

public int size()
Returns the number of elements in this Set (its cardinality).

Specified by:
size in interface LongCollection
Specified by:
size in interface LongSet
Specified by:
size in class AbstractLongCollection
Returns:
the size of the set