java.util
Class HashSet
java.lang.Object
|
+--java.util.AbstractCollection
|
+--java.util.AbstractSet
|
+--java.util.HashSet
All Implemented Interfaces:
Set, Cloneable, Serializable, Set, Collection
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:Authors:- Jon Zeppieri
- Eric Blake <ebb9@email.byu.edu>
See Also:
HashSet
public HashSet()
Construct a new, empty HashSet whose backing HashMap has the default
capacity (11) and loadFacor (0.75).
HashSet
public HashSet(int initialCapacity)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and the default load factor (0.75).
Parameters:
Throws:
HashSet
public HashSet(int initialCapacity, float loadFactor)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and load factor.
Parameters:
Throws:
HashSet
public HashSet(java.util.Collection 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:
Throws:
add
public boolean add(java.lang.Object o)
Adds the given Object to the set if it is not already in the Set.
This set permits a null element.
Parameters:
Returns:
- true if the set did not already contain o
clear
public void clear()
Empties this Set of all elements; this takes constant time.
clone
public Object clone()
Returns a shallow copy of this Set. The Set itself is cloned; its
elements are not.
Returns:
- a shallow clone of the set
contains
public boolean contains(java.lang.Object o)
Returns true if the supplied element is in this Set.
Parameters:
Returns:
isEmpty
public boolean isEmpty()
Returns true if this set has no elements in it.
Returns:
iterator
public Iterator 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.
Returns:
See Also:
remove
public boolean remove(java.lang.Object o)
Removes the supplied Object from this Set if it is in the Set.
Parameters:
Returns:
- true if an element was removed
size
public int size()
Returns the number of elements in this Set (its cardinality).
Returns:
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.