Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
Here,Especifies the type of objects that the queue will hold. The methods defined by Queue
are shown in Table 17-5.
Several methods throw aClassCastExceptionwhen an object is incompatible with the
elements in the queue. ANullPointerExceptionis thrown if an attempt is made to store a
nullobject andnullelements are not allowed in the queue. AnIllegalArgumentException
is thrown if an invalid argument is used. AnIllegalStateExceptionis thrown if an attempt is
made to add an element to a fixed-length queue that is full. ANoSuchElementException
is thrown if an attempt is made to remove an element from an empty queue.
Despite its simplicity,Queueoffers 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( )andremove( ). The difference between them is thatpoll( )returnsnullif the
queue is empty, butremove( )throws an exception. Third, there are two methods,element( )
andpeek( ), that obtain but don’t remove the element at the head of the queue. They differ
only in thatelement( )throws an exception if the queue is empty, butpeek( )returnsnull.
Finally, notice thatoffer( )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


TheDequeinterface was added by Java SE 6. It extendsQueueand 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.Dequeis a generic interface that has this declaration:

interface Deque<E>

Here,Especifies the type of objects that the deque will hold. In addition to the methods that
it inherits fromQueue,Dequeadds those methods summarized in Table 17-6. Several
methods throw aClassCastExceptionwhen an object is incompatible with the elements in
the deque. ANullPointerExceptionis thrown if an attempt is made to store anullobject
andnullelements are not allowed in the deque. AnIllegalArgumentExceptionis thrown if
an invalid argument is used. AnIllegalStateExceptionis thrown if an attempt is made to
add an element to a fixed-length deque that is full. ANoSuchElementExceptionis thrown
if an attempt is made to remove an element from an empty deque.
Notice thatDequeincludes the methodspush( )andpop( ). These methods enable aDeque
to function as a stack. Also, notice thedescendingIterator( )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. ADequeimplementation can becapacity-restricted,which means

446 Part II: The Java Library


Method Description
E element( ) Returns the element at the head of the queue. The element is not removed. It throws
NoSuchElementExceptionif the queue is empty.
boolean offer(Eobj) Attempts to addobjto the queue. Returnstrueifobjwas added andfalseother wise.
E peek( ) Returns the element at the head of the queue. It returnsnullif 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
returnsnullif the queue is empty.
E remove( ) Removes the element at the head of the queue, returning the element in the process.
It throwsNoSuchElementExceptionif the queue is empty.

TABLE 17-5 The Methods Defined byQueue
Free download pdf