java.util
public class HashSet<T> extends AbstractSet<T> implements Set<T>, Cloneable, Serializable
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
{@link ConcurrentModificationException} rather than exhibit
non-deterministic behavior.
Since: 1.2
See Also: Collection Set TreeSet synchronizedSet HashMap LinkedHashSet
UNKNOWN: updated to 1.4
Constructor Summary | |
---|---|
HashSet()
Construct a new, empty HashSet whose backing HashMap has the default
capacity (11) and loadFacor (0.75). | |
HashSet(int initialCapacity)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and the default load factor (0.75).
| |
HashSet(int initialCapacity, float loadFactor)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and load factor.
| |
HashSet(Collection<? extends T> c)
Construct a new HashSet with the same elements as are in the supplied
collection (eliminating any duplicates, of course). |
Method Summary | |
---|---|
boolean | add(T o)
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. |
Object | clone()
Returns a shallow copy of this Set. |
boolean | contains(Object o)
Returns true if the supplied element is in this Set.
|
boolean | isEmpty()
Returns true if this set has no elements in it.
|
Iterator<T> | iterator()
Returns an Iterator over the elements of this Set, which visits the
elements in no particular order. |
boolean | remove(Object o)
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).
|
Parameters: initialCapacity the initial capacity of the backing HashMap
Throws: IllegalArgumentException if the capacity is negative
Parameters: initialCapacity the initial capacity of the backing HashMap loadFactor the load factor of the backing HashMap
Throws: IllegalArgumentException if either argument is negative, or if loadFactor is POSITIVE_INFINITY or NaN
Parameters: c a collection of initial set elements
Throws: NullPointerException if c is null
Parameters: o the Object to add to this Set
Returns: true if the set did not already contain o
Returns: a shallow clone of the set
Parameters: o the Object to look for
Returns: true if it is in the set
Returns: size() == 0
.
Returns: a set iterator
See Also: ConcurrentModificationException
Parameters: o the object to remove
Returns: true if an element was removed
Returns: the size of the set