Java Reference
In-Depth Information
21.7.
Queue
The
Queue<E>
interface extends
Collection<E>
to add some structure to the
internal organization of the collection. A queue defines a
head
position,
which is the next element that would be removed. Queues often operate
on a
first-in-first-out
ordering, but it is also possible to have
last-in-first-
out
ordering (commonly known as a
stack
) or to have a specific ordering
defined by a comparator or by comparable elements. Each implementa-
tion must specify its ordering properties. The
Queue
interface adds several
methods that work specifically with the head:
public E
element()
Returns, but does not remove, the head of the queue. If the
queue is empty a
NoSuchElementException
is thrown.
public E
peek()
Returns, but does not remove, the head of the queue. If the
queue is empty,
null
is returned. This differs from
element
only
in its handling of an empty queue.
public E
remove()
Returns and removes the head of the queue. If the queue is
empty a
NoSuchElementException
is thrown.
public E
poll()
Returns and removes the head of the queue. If the queue is
empty,
null
is returned. This differs from
remove
only in its
handling of an empty queue.
There is also a method for inserting into a queue:
public boolean
offer(E elem)