# Class HashSet<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
java.util.HashSet<E>

All Implemented Interfaces:
  `Iterable<E>, Collection<E>, Set<E>`

Direct Known Subclasses:
  `LinkedHashSet`

public class HashSet<E> extends AbstractSet<E> implements Set<E>
HashSet is an implementation of a Set. All optional operations (adding and removing) are supported. The elements can be any objects.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `HashSet()`
  Constructs a new empty instance of `HashSet`.
  `HashSet(int capacity)`
  Constructs a new instance of `HashSet` with the specified capacity.
  `HashSet(int capacity, float loadFactor)`
  Constructs a new instance of `HashSet` with the specified capacity and load factor.
  `HashSet(Collection<? extends E> collection)`
  Constructs a new instance of `HashSet` containing the unique elements in the specified collection.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `add(E object)`
  Adds the specified object to this `HashSet` if not already present.
  `void`
  `clear()`
  Removes all elements from this `HashSet`, leaving it empty.
  `boolean`
  `contains(Object object)`
  Searches this `HashSet` for the specified object.
  `boolean`
  `isEmpty()`
  Returns true if this `HashSet` has no elements, false otherwise.
  `Iterator<E>`
  `iterator()`
  Returns an Iterator on the elements of this `HashSet`.
  `boolean`
  `remove(Object object)`
  Removes the specified object from this `HashSet`.
  `int`
  `size()`
  Returns the number of elements in this `HashSet`.

  ### Methods inherited from class AbstractSet

  `equals, hashCode, removeAll`

  ### Methods inherited from class AbstractCollection

  `addAll, containsAll, retainAll, toArray, toArray, toString`

  ### Methods inherited from class Object

  `clone, getClass, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface Collection

  `removeIf`

  ### Methods inherited from interface Iterable

  `forEach`

  ### Methods inherited from interface Set

  `addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray`

- ## Constructor Details

  - ### HashSet

    public HashSet()
    Constructs a new empty instance of `HashSet`.

  - ### HashSet

    public HashSet(int capacity)

    Constructs a new instance of `HashSet` with the specified capacity.

    ###### Parameters

    - `capacity`: the initial capacity of this `HashSet`.

  - ### HashSet

    public HashSet(int capacity, float loadFactor)

    Constructs a new instance of `HashSet` with the specified capacity and load factor.

    ###### Parameters

    - `capacity`: the initial capacity.

    - `loadFactor`: the initial load factor.

  - ### HashSet

    public HashSet(Collection<? extends E> collection)

    Constructs a new instance of `HashSet` containing the unique elements in the specified collection.

    ###### Parameters

    - `collection`: the collection of elements to add.

- ## Method Details

  - ### add

    public boolean add(E object)

    Adds the specified object to this `HashSet` if not already present.

    ###### Parameters

    - `object`: the object to add.

    ###### Returns

    Specified by:
      `add` in interface `Collection<E>`
    Specified by:
      `add` in interface `Set<E>`
    Overrides:
      `add` in class `AbstractCollection<E>`
    Returns:
      `true` when this `HashSet` did not already contain the object, `false` otherwise

  - ### clear

    public void clear()

    Removes all elements from this `HashSet`, leaving it empty.

    ###### See also

    - #isEmpty

    - #size

    Specified by:
      `clear` in interface `Collection<E>`
    Specified by:
      `clear` in interface `Set<E>`
    Overrides:
      `clear` in class `AbstractCollection<E>`

  - ### contains

    public boolean contains(Object object)

    Searches this `HashSet` for the specified object.

    ###### Parameters

    - `object`: the object to search for.

    ###### Returns

    Specified by:
      `contains` in interface `Collection<E>`
    Specified by:
      `contains` in interface `Set<E>`
    Overrides:
      `contains` in class `AbstractCollection<E>`
    Returns:
      `true` if `object` is an element of this `HashSet`, `false` otherwise.

  - ### isEmpty

    public boolean isEmpty()

    Returns true if this `HashSet` has no elements, false otherwise.

    ###### Returns

    Specified by:
      `isEmpty` in interface `Collection<E>`
    Specified by:
      `isEmpty` in interface `Set<E>`
    Overrides:
      `isEmpty` in class `AbstractCollection<E>`
    Returns:

      `true` if this `HashSet` has no elements, `false` otherwise.

      ###### See also

      - #size

  - ### iterator

    public Iterator<E> iterator()

    Returns an Iterator on the elements of this `HashSet`.

    ###### Returns

    an Iterator on the elements of this `HashSet`.

    ###### See also

    - Iterator
    Specified by:
      `iterator` in interface `Collection<E>`
    Specified by:
      `iterator` in interface `Iterable<E>`
    Specified by:
      `iterator` in interface `Set<E>`
    Specified by:
      `iterator` in class `AbstractCollection<E>`

  - ### remove

    public boolean remove(Object object)

    Removes the specified object from this `HashSet`.

    ###### Parameters

    - `object`: the object to remove.

    ###### Returns

    `true` if the object was removed, `false` otherwise.

    Specified by:
      `remove` in interface `Collection<E>`
    Specified by:
      `remove` in interface `Set<E>`
    Overrides:
      `remove` in class `AbstractCollection<E>`
    Returns:

      `true` if this `Collection` is modified, `false` otherwise.

      ###### Throws

      - `UnsupportedOperationException`: if removing from this `Collection` is not supported.

      - `ClassCastException`: if the object passed is not of the correct type.

      - `NullPointerException`: @throws NullPointerException if `object` is `null` and this `Collection` doesn't support `null` elements.

  - ### size

    public int size()

    Returns the number of elements in this `HashSet`.

    ###### Returns

    the number of elements in this `HashSet`.

    Specified by:
      `size` in interface `Collection<E>`
    Specified by:
      `size` in interface `Set<E>`
    Specified by:
      `size` in class `AbstractCollection<E>`
    Returns:
      how many objects this `Collection` contains, or `Integer.MAX_VALUE` if there are more than `Integer.MAX_VALUE` elements in this `Collection`.