Java Reference
In-Depth Information
Whenadequeisusedasaqueue,FIFO(First-In-First-Out)behaviorresults.Elements
areaddedattheendofthedequeandremovedfromthebeginning.Themethodsinher-
itedfromthe
Queue
interfacearepreciselyequivalenttothe
Deque
methodsasindic-
ated in
Table 5-7
.
Finally, deques can also be used as LIFO (Last-In-First-Out) stacks. When a deque
is used as a stack, elements are pushed and popped from the beginning of the deque.
Becauseastack's
push(e)
methodwouldbeequivalentto
Deque
's
addFirst(e)
method,its
pop()
methodwouldbeequivalentto
Deque
's
removeFirst()
meth-
od,andits
peek()
methodwouldbeequivalentto
Deque
's
peekFirst()
method,
Deque
declaresthe
E peek()
,
E pop()
,and
void push(E e)
stack-oriented
convenience methods.
ArrayDeque
The
ArrayDeque
class provides a resizable-array implementation of the
Deque
in-
terface. It prohibits null elements from being added to a deque, and its
iterator()
method returns fail-fast iterators.
ArrayDeque
supplies three constructors:
•
ArrayDeque()
createsanemptyarraylistwithaninitialcapacityof16ele-
ments.