Class 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
    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
    Removes all elements from this HashSet, leaving it empty.
    boolean
    contains(Object object)
    Searches this HashSet for the specified object.
    boolean
    Returns true if this HashSet has no elements, false otherwise.
    Returns an Iterator on the elements of this HashSet.
    boolean
    remove(Object object)
    Removes the specified object from this HashSet.
    int
    Returns the number of elements in this HashSet.

    Methods inherited from class AbstractSet

    equals, hashCode, removeAll
    Modifier and Type
    Method
    Description
    boolean
    equals(Object object)
    Compares the specified object to this Set and returns true if they are equal.
    int
    Returns the hash code for this set.
    boolean
    removeAll(Collection<?> collection)
    Removes all occurrences in this collection which are contained in the specified collection.

    Methods inherited from class AbstractCollection

    addAll, containsAll, retainAll, toArray, toArray, toString
    Modifier and Type
    Method
    Description
    boolean
    addAll(Collection<? extends E> collection)
    Attempts to add all of the objects contained in collection to the contents of this Collection (optional).
    boolean
    containsAll(Collection<?> collection)
    Tests whether this Collection contains all objects contained in the specified Collection.
    boolean
    retainAll(Collection<?> collection)
    Removes all objects from this Collection that are not also found in the Collection passed (optional).
    Returns a new array containing all elements contained in this ArrayList.
    <T> T[]
    toArray(T[] contents)
    Returns an array containing all elements contained in this ArrayList.
    Returns the string representation of this Collection.

    Methods inherited from class Object

    clone, getClass, notify, notifyAll, wait, wait, wait
    Modifier and Type
    Method
    Description
    protected Object
     
    final Class
    Returns the runtime class of an object.
    final void
    Wakes up a single thread that is waiting on this object's monitor.
    final void
    Wakes up all threads that are waiting on this object's monitor.
    final void
    Causes current thread to wait until another thread invokes the method or the method for this object.
    final void
    wait(long timeout)
    Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.
    final void
    wait(long timeout, int nanos)
    Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

    Methods inherited from interface Set

    addAll, containsAll, retainAll, toArray, toArray
    Modifier and Type
    Method
    Description
    boolean
    addAll(Collection<? extends E> collection)
    Adds the objects in the specified collection which do not exist yet in this set.
    boolean
    containsAll(Collection<?> collection)
    Searches this set for all objects in the specified collection.
    boolean
    retainAll(Collection<?> collection)
    Removes all objects from this set that are not contained in the specified collection.
    Returns an array containing all elements contained in this set.
    <T> T[]
    toArray(T[] array)
    Returns an array containing all elements contained in this set.
  • 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.
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
      Overrides:
      add in class AbstractCollection<E>
      Parameters:
      object - the object to add.
      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.
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface Set<E>
      Overrides:
      clear in class AbstractCollection<E>
      See Also:
    • contains

      public boolean contains(Object object)
      Searches this HashSet for the specified object.
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
      Overrides:
      contains in class AbstractCollection<E>
      Parameters:
      object - the object to search for.
      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.
      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:
    • iterator

      public Iterator<E> iterator()
      Returns an Iterator on the elements of this HashSet.
      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>
      Returns:
      an Iterator on the elements of this HashSet.
      See Also:
    • remove

      public boolean remove(Object object)
      Removes the specified object from this HashSet.
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Set<E>
      Overrides:
      remove in class AbstractCollection<E>
      Parameters:
      object - the object to remove.
      Returns:
      true if the object was removed, false otherwise.
    • size

      public int size()
      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:
      the number of elements in this HashSet.