HashSet

This class implements the <tt>Set</tt> interface, backed by a hash table (actually a <tt>HashMap</tt> 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 <tt>null</tt> element.

<p>This class offers constant time performance for the basic operations (<tt>add</tt>, <tt>remove</tt>, <tt>contains</tt> and <tt>size</tt>), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the <tt>HashSet</tt> instance's size (the number of elements) plus the "capacity" of the backing <tt>HashMap</tt> 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.

<p><strong>Note that this implementation is not synchronized.</strong> If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it <i>must</i> 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 {@link Collections#synchronizedSet Collections.synchronizedSet} method. This is best done at creation time, to prevent accidental unsynchronized access to the set:<pre> Set s = Collections.synchronizedSet(new HashSet(...));</pre>

<p>The iterators returned by this class's <tt>iterator</tt> method are <i>fail-fast</i>: if the set is modified at any time after the iterator is created, in any way except through the iterator's own <tt>remove</tt> method, the Iterator throws a {@link 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.

<p>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 <tt>ConcurrentModificationException</tt> on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: <i>the fail-fast behavior of iterators should be used only to detect bugs.</i>

<p>This class is a member of the <a href="{@docRoot}/../technotes/guides/collections/index.html"> Java Collections Framework</a>.

@param <E> the type of elements maintained by this set

@author Josh Bloch @author Neal Gafter @see Collection @see Set @see TreeSet @see HashMap

Constructors

this
this()

Constructs a new, empty set; the backing <tt>HashMap</tt> instance has default initial capacity (16) and load factor (0.75).

this
this(Collection!E c)

Constructs a new set containing the elements in the specified collection. The <tt>HashMap</tt> is created with default load factor (0.75) and an initial capacity sufficient to contain the elements in the specified collection.

this
this(E[] c)
Undocumented in source.
this
this(int initialCapacity, float loadFactor)

Constructs a new, empty set; the backing <tt>HashMap</tt> instance has the specified initial capacity and the specified load factor.

this
this(int initialCapacity)

Constructs a new, empty set; the backing <tt>HashMap</tt> instance has the specified initial capacity and default load factor (0.75).

this
this(int initialCapacity, float loadFactor, bool 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.

Members

Functions

add
bool add(E e)

Adds the specified element to this set if it is not already present. More formally, adds the specified element <tt>e</tt> to this set if this set contains no element <tt>e2</tt> such that <tt>(e is null&nbsp;?&nbsp;e2 is null&nbsp;:&nbsp;e.equals(e2))</tt>. If this set already contains the element, the call leaves the set unchanged and returns <tt>false</tt>.

clear
void clear()

Removes all of the elements from this set. The set will be empty after this call returns.

contains
bool contains(E o)

Returns <tt>true</tt> if this set contains the specified element. More formally, returns <tt>true</tt> if and only if this set contains an element <tt>e</tt> such that <tt>(o is null&nbsp;?&nbsp;e is null&nbsp;:&nbsp;o.equals(e))</tt>.

isEmpty
bool isEmpty()

Returns <tt>true</tt> if this set contains no elements.

iterator
InputRange!E iterator()

Returns an iterator over the elements in this set. The elements are returned in no particular order.

opApply
int opApply(int delegate(ref E) dg)

Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> and <em>fail-fast</em> {@link Spliterator} over the elements in this set.

opEquals
bool opEquals(IObject o)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(Object o)
Undocumented in source. Be warned that the author may not have intended to support it.
remove
bool remove(E o)

Removes the specified element from this set if it is present. More formally, removes an element <tt>e</tt> such that <tt>(o is null&nbsp;?&nbsp;e is null&nbsp;:&nbsp;o.equals(e))</tt>, if this set contains such an element. Returns <tt>true</tt> if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)

size
int size()

Returns the number of elements in this set (its cardinality).

toHash
size_t toHash()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.

Variables

map
HashMap!(E, Object) map;
Undocumented in source.

Meta