Class AbstractQueue<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
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.

Type parameter E: the type of the element in the collection.

  • 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.

      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.
      Specified by:
      add in interface Collection<E>
      Overrides:
      add in class AbstractCollection<E>
      Returns:

      true if this Collection is modified, false otherwise.

      Throws
      • UnsupportedOperationException: if adding to this Collection is not supported.

      • ClassCastException: @throws ClassCastException if the class of the object is inappropriate for this collection.

      • IllegalArgumentException: if the object cannot be added to this Collection.

      • NullPointerException: if null elements cannot be added to the Collection.

    • 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.

      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: @throws IllegalArgumentException If the collection to be added to the queue is the queue itself.

      Specified by:
      addAll in interface Collection<E>
      Overrides:
      addAll in class AbstractCollection<E>
      Returns:

      true if this Collection is modified, false otherwise.

      Throws
      • UnsupportedOperationException: if adding to this Collection is not supported.

      • ClassCastException: @throws ClassCastException if the class of an object is inappropriate for this Collection.

      • IllegalArgumentException: if an object cannot be added to this Collection.

      • NullPointerException: @throws NullPointerException if collection is null, or if it contains null elements and this Collection does not support such elements.

    • remove

      public E remove()

      Removes the element at the head of the queue and returns it.

      Returns

      the element at the head of the queue.

      Throws
      • NoSuchElementException: if the queue is empty.
      Specified by:
      remove in interface Queue<E>
    • element

      public E element()

      Returns but does not remove the element at the head of the queue.

      Returns

      the element at the head of the queue.

      Throws
      • NoSuchElementException: if the queue is empty.
      Specified by:
      element in interface Queue<E>
    • 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>