abstract class AbstractQueue<E : Any!> : AbstractCollection<E>, Queue<E>

This class provides skeletal implementations of some Queue operations. The implementations in this class are appropriate when the base implementation does not allow null elements. Methods #add, #remove, and element are based on offer, poll, and peek, respectively, but throw exceptions instead of indicating failure via false or null returns.

A Queue implementation that extends this class must minimally define a method Queue.offer which does not permit insertion of null elements, along with methods Queue.peek, Queue.poll, Collection.size, and Collection.iterator. Typically, additional methods will be overridden as well. If these requirements cannot be met, consider instead subclassing AbstractCollection.

Summary

Protected constructors

Constructor for use by subclasses.

Public methods
open Boolean

add(element: E)

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

open Boolean

addAll(elements: Collection<E>)

Adds all of the elements in the specified collection to this queue.

open Unit

clear()

Removes all of the elements from this queue.

open E

element()

Retrieves, but does not remove, the head of this queue.

open E

remove()

Retrieves and removes the head of this queue.

Inherited functions

From class AbstractCollection

Boolean contains(element: E?)

Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that Objects.equals(o, e).

Boolean containsAll(elements: Collection<E>)

Returns true if this collection contains all of the elements in the specified collection.

Boolean isEmpty()

Returns true if this collection contains no elements.

MutableIterator<E> iterator()

Returns an iterator over the elements contained in this collection.

Boolean remove(element: E?)

Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that Objects.equals(o, e), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).

Boolean removeAll(elements: Collection<E>)

Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.

Boolean retainAll(elements: Collection<E>)

Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.

Array<Any!> toArray()

Returns an array containing all of the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array's runtime component type is Object.

The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.

Array<T> toArray(a: Array<T>)

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.)

If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.

String toString()

Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

From class Queue

Boolean offer(e: E)

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add, which can fail to insert an element only by throwing an exception.

E? peek()

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

E? poll()

Retrieves and removes the head of this queue, or returns null if this queue is empty.

Inherited properties

From class AbstractCollection

Int size

Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Protected constructors

AbstractQueue

protected AbstractQueue()

Constructor for use by subclasses.

Public methods

add

open fun add(element: E): Boolean

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

This implementation returns true if offer succeeds, else throws an IllegalStateException.

Parameters
e the element to add
Return
Boolean true (as specified by Collection.add)
Exceptions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this queue
java.lang.IllegalArgumentException if some property of this element prevents it from being added to this queue
java.lang.IllegalStateException if the element cannot be added at this time due to capacity restrictions
java.lang.NullPointerException if the specified element is null and this queue does not permit null elements
java.lang.UnsupportedOperationException if the add operation is not supported by this collection

addAll

open fun addAll(elements: Collection<E>): Boolean

Adds all of the elements in the specified collection to this queue. Attempts to addAll of a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

This implementation iterates over the specified collection, and adds each element returned by the iterator to this queue, in turn. A runtime exception encountered while trying to add an element (including, in particular, a null element) may result in only some of the elements having been successfully added when the associated exception is thrown.

Parameters
c collection containing elements to be added to this queue
Return
Boolean true if this queue changed as a result of the call
Exceptions
java.lang.ClassCastException if the class of an element of the specified collection prevents it from being added to this queue
java.lang.IllegalArgumentException if some property of an element of the specified collection prevents it from being added to this queue, or if the specified collection is this queue
java.lang.IllegalStateException if not all the elements can be added at this time due to insertion restrictions
java.lang.NullPointerException if the specified collection contains a null element and this queue does not permit null elements, or if the specified collection is null
java.lang.UnsupportedOperationException if the addAll operation is not supported by this collection

clear

open fun clear(): Unit

Removes all of the elements from this queue. The queue will be empty after this call returns.

This implementation repeatedly invokes poll until it returns null.

Exceptions
java.lang.UnsupportedOperationException if the clear operation is not supported by this collection

element

open fun element(): E

Retrieves, but does not remove, the head of this queue. This method differs from peek only in that it throws an exception if this queue is empty.

This implementation returns the result of peek unless the queue is empty.

Return
E the head of this queue
Exceptions
java.util.NoSuchElementException if this queue is empty

remove

open fun remove(): E

Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.

This implementation returns the result of poll unless the queue is empty.

Return
E the head of this queue
Exceptions
java.util.NoSuchElementException if this queue is empty

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2026-02-13 UTC.