The Queue interface extends Collection and declares the behavior of a queue, which is often a first-in, first-out list. However, there are types of queues in which the ordering is based upon other criteria. Queue is a generic interface that has this declaration:
interface Queue<E>
Method | Description | |||
E element( ) | Returns the element at the head of the queue. The element is not removed. It throws | |||
NoSuchElementException if the queue is empty. | ||||
boolean offer(E obj) | Attempts to add obj to the queue. Returns true if obj was added and false otherwise. | |||
E peek( ) | Returns the element at the head of the queue. It returns null if the queue is empty. | |||
The element is not removed. | ||||
E poll( ) | Returns the element at the head of the queue, removing the element in the process. It | |||
returns null if the queue is empty. | ||||
E remove( ) | Removes the element at the head of the queue, returning the element in the process. | |||
It throws NoSuchElementException if the queue is empty. | ||||
Here, E specifies the type of objects that the queue will hold. The methods defined by Queue | ||||
Several methods throw a ClassCastException when an object is incompatible with the | ||||
elements in the queue. A NullPointerException is thrown if an attempt is made to store a | ||||
null object and null elements are not allowed in the queue. An IllegalArgumentException | ||||
is thrown if an invalid argument is used. An IllegalStateException is thrown if an attempt is | ||||
made to add an element to a fixed-length queue that is full. A NoSuchElementException | ||||
is thrown if an attempt is made to remove an element from an empty queue. | ||||
Despite its simplicity, Queue offers several points of interest. First, elements can only be | ||||
removed from the head of the queue. Second, there are two methods that obtain and remove | ||||
elements: poll( ) and remove( ). The difference between them is that poll( ) returns null if the | ||||
queue is empty, but remove( ) throws an exception. Third, there are two methods, element( ) | ||||
and peek( ), that obtain but don’t remove the element at the head of the queue. They differ | ||||
only in that element( ) throws an exception if the queue is empty, but peek( ) returns null. | ||||
Finally, notice that offer( ) only attempts to add an element to a queue. Because some queues | ||||
have a fixed length and might be full, offer( ) can fail. | ||||
The Deque Interface | ||||
The Deque interface was added by Java SE 6. It extends Queue and declares the behavior of | ||||
a double-ended queue. Double-ended queues can function as standard, first-in, first-out | ||||
queues or as last-in, first-out stacks. Deque is a generic interface that has this declaration: | ||||
interface Deque<E> | ||||
Here, E specifies the type of objects that the deque will hold. In addition to the methods that | ||||
it inherits from Queue, Deque adds those methods summarized in Table 17-6. Several | ||||
methods throw a ClassCastException when an object is incompatible with the elements in | ||||
the deque. A NullPointerException is thrown if an attempt is made to store a null object | ||||
and null elements are not allowed in the deque. An IllegalArgumentException is thrown if | ||||
an invalid argument is used. An IllegalStateException is thrown if an attempt is made to | ||||
add an element to a fixed-length deque that is full. A NoSuchElementException is thrown | ||||
if an attempt is made to remove an element from an empty deque. | ||||
Notice that Deque includes the methods push( ) and pop( ). These methods enable a Deque | ||||
to function as a stack. Also, notice the descendingIterator( ) method. It returns an iterator that | ||||
returns elements in reverse order. In other words, it returns an iterator that moves from the end | ||||
of the collection to the start. A Deque implementation can be capacity-restricted, which means |