# Class AbstractSet<E>

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

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

Direct Known Subclasses:
  `HashSet, TreeSet`

public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E>
An AbstractSet is an abstract implementation of the Set interface. This implementation does not support adding. A subclass must implement the abstract methods iterator() and size().

Since:
  1.2

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `AbstractSet()`
  Constructs a new instance of this AbstractSet.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `equals(Object object)`
  Compares the specified object to this Set and returns true if they are equal.
  `int`
  `hashCode()`
  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

  `add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, retainAll, size, toArray, toArray, toString`
  Modifier and Type
  Method
  Description
  `boolean`
  `add(E object)`
  Attempts to add `object` to the contents of this `Collection` (optional).
  `boolean`
  `addAll(Collection<? extends E> collection)`
  Attempts to add all of the objects contained in `collection` to the contents of this `Collection` (optional).
  `void`
  `clear()`
  Removes all elements from this `Collection`, leaving it empty (optional).
  `boolean`
  `contains(Object object)`
  Tests whether this `Collection` contains the specified object.
  `boolean`
  `containsAll(Collection<?> collection)`
  Tests whether this `Collection` contains all objects contained in the specified `Collection`.
  `boolean`
  `isEmpty()`
  Returns if this `Collection` contains no elements.
  `abstract Iterator<E>`
  `iterator()`
  Returns an instance of `Iterator` that may be used to access the objects contained by this `Collection`.
  `boolean`
  `remove(Object object)`
  Removes one instance of the specified object from this `Collection` if one is contained (optional).
  `boolean`
  `retainAll(Collection<?> collection)`
  Removes all objects from this `Collection` that are not also found in the `Collection` passed (optional).
  `abstract int`
  `size()`
  Returns a count of how many objects this `Collection` contains.
  `Object[]`
  `toArray()`
  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`.
  `String`
  `toString()`
  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`
  `clone()`
  `final Class`
  `getClass()`
  Returns the runtime class of an object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `final void`
  `wait()`
  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

  `add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, retainAll, size, toArray, toArray`
  Modifier and Type
  Method
  Description
  `boolean`
  `add(E object)`
  Adds the specified object to this set.
  `boolean`
  `addAll(Collection<? extends E> collection)`
  Adds the objects in the specified collection which do not exist yet in this set.
  `void`
  `clear()`
  Removes all elements from this set, leaving it empty.
  `boolean`
  `contains(Object object)`
  Searches this set for the specified object.
  `boolean`
  `containsAll(Collection<?> collection)`
  Searches this set for all objects in the specified collection.
  `boolean`
  `isEmpty()`
  Returns true if this set has no elements.
  `Iterator<E>`
  `iterator()`
  Returns an iterator on the elements of this set.
  `boolean`
  `remove(Object object)`
  Removes the specified object from this set.
  `boolean`
  `retainAll(Collection<?> collection)`
  Removes all objects from this set that are not contained in the specified collection.
  `int`
  `size()`
  Returns the number of elements in this set.
  `Object[]`
  `toArray()`
  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

  - ### AbstractSet

    protected AbstractSet()
    Constructs a new instance of this AbstractSet.

- ## Method Details

  - ### equals

    public boolean equals(Object object)
    Compares the specified object to this Set and returns true if they are equal. The object must be an instance of Set and contain the same objects.
    Specified by:
      `equals` in interface `Collection<E>`
    Specified by:
      `equals` in interface `Set<E>`
    Overrides:
      `equals` in class `Object`
    Parameters:
      `object` - the object to compare with this set.
    Returns:
      `true` if the specified object is equal to this set, `false` otherwise
    See Also:
      - `hashCode()`

  - ### hashCode

    public int hashCode()
    Returns the hash code for this set. Two set which are equal must return the same value. This implementation calculates the hash code by adding each element's hash code.
    Specified by:
      `hashCode` in interface `Collection<E>`
    Specified by:
      `hashCode` in interface `Set<E>`
    Overrides:
      `hashCode` in class `Object`
    Returns:
      the hash code of this set.
    See Also:
      - `equals(Object)`

  - ### removeAll

    public boolean removeAll(Collection<?> collection)
    Removes all occurrences in this collection which are contained in the specified collection.
    Specified by:
      `removeAll` in interface `Collection<E>`
    Specified by:
      `removeAll` in interface `Set<E>`
    Overrides:
      `removeAll` in class `AbstractCollection<E>`
    Parameters:
      `collection` - the collection of objects to remove.
    Returns:
      `true` if this collection was modified, `false` otherwise.
    Throws:
      `UnsupportedOperationException` - if removing from this collection is not supported.