# Class AbstractQueue<E>

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

Type Parameters:
  `E` - the type of the element in the collection.

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

Direct Known Subclasses:
  `PriorityQueue`

public abstract class AbstractQueue<E> extends AbstractCollection<E> implements Queue<E>
AbstractQueue is an abstract class which implements some of the methods in `Queue`. The provided implementations of `add, remove` and `element` are based on `offer, poll`, and `peek` except that they throw exceptions to indicate some error instead of returning true or false.

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `AbstractQueue()`
  Constructor to be used by subclasses.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `add(E o)`
  Adds an element to the queue.
  `boolean`
  `addAll(Collection<? extends E> c)`
  Adds all the elements of a collection to the queue.
  `void`
  `clear()`
  Removes all elements of the queue, leaving it empty.
  `E`
  `element()`
  Returns but does not remove the element at the head of the queue.
  `E`
  `remove()`
  Removes the element at the head of the queue and returns it.

  ### Methods inherited from class AbstractCollection

  `contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString`
  Modifier and Type
  Method
  Description
  `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`
  `removeAll(Collection<?> collection)`
  Removes all occurrences in this `Collection` of each object in the specified `Collection` (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, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `final Class`
  `getClass()`
  Returns the runtime class of an object.
  `int`
  `hashCode()`
  Returns a hash code value for the 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 Collection

  `equals, hashCode`
  Modifier and Type
  Method
  Description
  `boolean`
  `equals(Object object)`
  Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.
  `int`
  `hashCode()`
  Returns an integer hash code for the receiver.

  ### Methods inherited from interface Queue

  `offer, peek, poll`
  Modifier and Type
  Method
  Description
  `boolean`
  `offer(E o)`
  Inserts the specified element into the queue provided that the condition allows such an operation.
  `E`
  `peek()`
  Gets but does not remove the element at the head of the queue.
  `E`
  `poll()`
  Gets and removes the element at the head of the queue, or returns `null` if there is no element in the queue.

- ## Constructor Details

  - ### AbstractQueue

    protected AbstractQueue()
    Constructor to be used by subclasses.

- ## Method Details

  - ### add

    public boolean add(E o)
    Adds an element to the queue.
    Specified by:
      `add` in interface `Collection<E>`
    Overrides:
      `add` in class `AbstractCollection<E>`
    Parameters:
      `o` - the element to be added to the queue.
    Returns:
      `true` if the operation succeeds, otherwise `false`.
    Throws:
      `IllegalStateException` - if the element is not allowed to be added to the queue.

  - ### addAll

    public boolean addAll(Collection<? extends E> c)
    Adds all the elements of a collection to the queue. If the collection is the queue itself, then an IllegalArgumentException will be thrown. If during the process, some runtime exception is thrown, then those elements in the collection which have already successfully been added will remain in the queue. The result of the method is undefined if the collection is modified during the process of the method.
    Specified by:
      `addAll` in interface `Collection<E>`
    Overrides:
      `addAll` in class `AbstractCollection<E>`
    Parameters:
      `c` - the collection to be added to the queue.
    Returns:
      `true` if the operation succeeds, otherwise `false`.
    Throws:
      `NullPointerException` - if the collection or any element of it is null.
      `IllegalArgumentException` - If the collection to be added to the queue is the queue itself.

  - ### remove

    public E remove()
    Removes the element at the head of the queue and returns it.
    Specified by:
      `remove` in interface `Queue<E>`
    Returns:
      the element at the head of the queue.
    Throws:
      `NoSuchElementException` - if the queue is empty.

  - ### element

    public E element()
    Returns but does not remove the element at the head of the queue.
    Specified by:
      `element` in interface `Queue<E>`
    Returns:
      the element at the head of the queue.
    Throws:
      `NoSuchElementException` - if the queue is empty.

  - ### clear

    public void clear()
    Removes all elements of the queue, leaving it empty.
    Specified by:
      `clear` in interface `Collection<E>`
    Overrides:
      `clear` in class `AbstractCollection<E>`
    See Also:
      - `AbstractCollection.iterator()`
      - `AbstractCollection.isEmpty()`
      - `AbstractCollection.size()`