Set
interface, backed by a hash table
(actually a HashMap
instance). It 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 permits the null
element.
This class offers constant time performance for the basic operations
(add
, remove
, contains
and size
),
assuming the hash function disperses the elements properly among the
buckets. Iterating over this set requires time proportional to the sum of
the HashSet
instance's size (the number of elements) plus the
"capacity" of the backing HashMap
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 implementation is not synchronized.
If multiple threads access a hash set concurrently, and at least one of
the threads modifies the set, it must be synchronized externally.
This is typically accomplished by synchronizing on some object that
naturally encapsulates the set.
If no such object exists, the set should be "wrapped" using the
Collections.
method. This is best done at creation time, to prevent accidental
unsynchronized access to the set:
Set s = Collections.synchronizedSet(new HashSet(...));
The iterators returned by this class's iterator
method are
fail-fast: if the set is modified at any time after the iterator is
created, in any way except through the iterator's own remove
method, the Iterator throws a ConcurrentModificationException
.
Thus, in the face of concurrent modification, the iterator fails quickly
and cleanly, rather than risking arbitrary, non-deterministic behavior at
an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed
as it is, generally speaking, impossible to make any hard guarantees in the
presence of unsynchronized concurrent modification. Fail-fast iterators
throw ConcurrentModificationException
on a best-effort basis.
Therefore, it would be wrong to write a program that depended on this
exception for its correctness: the fail-fast behavior of iterators
should be used only to detect bugs.
This class is a member of the Java Collections Framework.
Collection
, Set
, TreeSet
, HashMap
Modifier and Type | Field and Description |
---|---|
pack-priv transient HashMap | |
pack-priv static final Object | |
pack-priv static final long |
Access | Constructor and Description |
---|---|
public | HashSet()
Constructs a new, empty set; the backing |
public | HashSet(Collection<? extends E>
the collection whose elements are to be placed into this set c)Constructs a new set containing the elements in the specified collection. |
public | HashSet(int
the initial capacity of the hash map initialCapacity, float the load factor of the hash map loadFactor)Constructs a new, empty set; the backing |
public | HashSet(int
the initial capacity of the hash table initialCapacity)Constructs a new, empty set; the backing |
pack-priv | HashSet(int
the initial capacity of the hash map initialCapacity, float the load factor of the hash map loadFactor, boolean ignored (distinguishes this
constructor from other int, float constructor.) dummy)Constructs a new, empty linked hash set. |
Modifier and Type | Method and Description |
---|---|
public boolean | Returns: true if this set did not already contain the specified
elementelement to be added to this set e)Overrides java. Implements java. Adds the specified element to this set if it is not already present. |
public void | clear()
Overrides java. Implements java. Removes all of the elements from this set. |
public Object | Returns: a shallow copy of this setOverrides java. Returns a shallow copy of this |
public boolean | Returns: true if this set contains the specified elementelement whose presence in this set is to be tested o)Overrides java. Implements java. Returns |
public boolean | Returns: true if this set contains no elementsOverrides java. Implements java. Returns |
public Iterator | Returns: an Iterator over the elements in this setImplements abstract java. Implements java. Returns an iterator over the elements in this set. |
public static < the type of elements maintained by the new set T> HashSet | Returns: the newly created setthe expected number of elements numElements)Creates a new, empty HashSet suitable for the expected number of elements. |
private void | readObject(ObjectInputStream s)
Reconstitute the |
public boolean | Returns: true if the set contained the specified elementobject to be removed from this set, if present o)Overrides java. Implements java. Removes the specified element from this set if it is present. |
public int | Returns: the number of elements in this set (its cardinality)Implements abstract java. Implements java. Returns the number of elements in this set (its cardinality). |
public Spliterator | Returns: aSpliterator over the elements in this setOverrides default java. Creates a late-binding
and fail-fast |
public Object[] | toArray()
Overrides java. Implements java. Returns an array containing all of the elements in this set. |
public <T> T[] | toArray(T[]
the array into which the elements of this set are to be
stored, if it is big enough; otherwise, a new array of the same
runtime type is allocated for this purpose. a)Overrides java. Implements java. Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. |
private void | writeObject(ObjectOutputStream s)
Save the state of this |
map | back to summary |
---|---|
pack-priv transient HashMap<E, Object> map |
PRESENT | back to summary |
---|---|
pack-priv static final Object PRESENT |
serialVersionUID | back to summary |
---|---|
pack-priv static final long serialVersionUID
|
HashSet | back to summary |
---|---|
public HashSet() Constructs a new, empty set; the backing |
HashSet | back to summary |
---|---|
public HashSet(Collection<? extends E> c) Constructs a new set containing the elements in the specified
collection. The
|
HashSet | back to summary |
---|---|
public HashSet(int initialCapacity, float loadFactor) Constructs a new, empty set; the backing API Note To create a
|
HashSet | back to summary |
---|---|
public HashSet(int initialCapacity) Constructs a new, empty set; the backing API Note To create a
|
HashSet | back to summary |
---|---|
pack-priv HashSet(int initialCapacity, float loadFactor, boolean dummy) Constructs a new, empty linked hash set. (This package private constructor is only used by LinkedHashSet.) The backing HashMap instance is a LinkedHashMap with the specified initial capacity and the specified load factor.
|
add | back to summary |
---|---|
public boolean add(E e) Overrides java. Implements java. Adds the specified element to this set if it is not already present.
More formally, adds the specified element
|
clear | back to summary |
---|---|
public void clear() Overrides java. Implements java. Removes all of the elements from this set. The set will be empty after this call returns. |
clone | back to summary |
---|---|
public Object clone() Overrides java. Returns a shallow copy of this
|
contains | back to summary |
---|---|
public boolean contains(Object o) Overrides java. Implements java. Returns
|
isEmpty | back to summary |
---|---|
public boolean isEmpty() Overrides java. Implements java. Returns
|
iterator | back to summary |
---|---|
public Iterator Implements abstract java. Implements java. Returns an iterator over the elements in this set. The elements are returned in no particular order.
|
newHashSet | back to summary |
---|---|
public static <T> HashSet Creates a new, empty HashSet suitable for the expected number of elements. The returned set uses the default load factor of 0.75, and its initial capacity is generally large enough so that the expected number of elements can be added without resizing the set.
|
readObject | back to summary |
---|---|
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException Reconstitute the
|
remove | back to summary |
---|---|
public boolean remove(Object o) Overrides java. Implements java. Removes the specified element from this set if it is present.
More formally, removes an element
|
size | back to summary |
---|---|
public int size() Implements abstract java. Implements java. Returns the number of elements in this set (its cardinality).
|
spliterator | back to summary |
---|---|
public Spliterator Overrides default java. Creates a late-binding
and fail-fast The
|
toArray | back to summary |
---|---|
public Object[] toArray() Overrides java. Implements java. Doc from java. Returns an array containing all of the elements in this set. If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array will be "safe" in that no references to it are maintained by this set. (In other words, this method must allocate a new array even if this set is backed by an array). The caller is thus free to modify the returned array. This method acts as bridge between array-based and collection-based APIs. |
toArray | back to summary |
---|---|
public <T> T[] toArray(T[] a) Overrides java. Implements java. Doc from java. Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. If the set fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this set. If this set fits in the specified array with room to spare
(i.e., the array has more elements than this set), the element in
the array immediately following the end of the set is set to
If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. Like the Suppose String[] y = x.toArray(new String[0]);Note that toArray(new Object[0]) is identical in function to
toArray() .
|
writeObject | back to summary |
---|---|
private void writeObject(ObjectOutputStream s) throws IOException Save the state of this
|