Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Chapter 17: java.util Part 1: The Collections Framework 447


that only a limited number of elements can be added to the deque. When this is the case, an
attempt to add an element to the deque can fail.Dequeallows you to handle such a failure in
two ways. First, methods such asaddFirst( )andaddLast( )throw anIllegalStateExceptionif a


Method Description
void addFirst(Eobj) Addsobjto the head of the deque. Throws anIllegalStateException
if a capacity-restricted deque is out of space.
void addLast(Eobj) Addsobjto the tail of the deque. Throws anIllegalStateException
if a capacity-restricted deque is out of space.
Iterator<E> descendingIterator( ) Returns an iterator that moves from the tail to the head of the
deque. In other words, it returns a reverse iterator.
E getFirst( ) Returns the first element in the deque. The object is not removed
from the deque. It throwsNoSuchElementExceptionif the deque
is empty.
E getLast( ) Returns the last element in the deque. The object is not removed
from the deque. It throwsNoSuchElementExceptionif the deque is
empty.
boolean offerFirst(Eobj) Attempts to addobjto the head of the deque. Returnstrueif
objwas added andfalseother wise. Therefore, this method
returnsfalsewhen an attempt is made to addobjto a full,
capacity-restricted deque.
boolean offerLast(Eobj) Attempts to addobjto the tail of the deque. Returnstrueifobj
was added andfalseother wise.
E peekFirst( ) Returns the element at the head of the deque. It returnsnullif
the deque is empty. The object is not removed.
E peekLast( ) Returns the element at the tail of the deque. It returnsnullif the
deque is empty. The object is not removed.
E pollFirst( ) Returns the element at the head of the deque, removing the
element in the process. It returnsnullif the deque is empty.
E pollLast( ) Returns the element at the tail of the deque, removing the
element in the process. It returnsnullif the deque is empty.
E pop( ) Returns the element at the head of the deque, removing it in the
process. It throwsNoSuchElementExceptionif the deque is empty.
void push(Eobj) Addsobjto the head of the deque. Throws anIllegalStateException
if a capacity-restricted deque is out of space.
E removeFirst( ) Returns the element at the head of the deque, removing the
element in the process. It throwsNoSuchElementExceptionif
the deque is empty.
boolean
removeFirstOccurrence(Objectobj)

Removes the first occurrence ofobjfrom the deque. Returnstrue
if successful andfalseif the deque did not containobj.
E removeLast( ) Returns the element at the tail of the deque, removing the element
in the process. It throwsNoSuchElementExceptionif the deque
is empty.
boolean
removeLastOccurrence(Objectobj)

Removes the last occurrence ofobjfrom the deque. Returnstrue
if successful andfalseif the deque did not containobj.

TABLE 17-6 The Methods Defined byDeque

Free download pdf