Java Reference
In-Depth Information
the removeFirst() method. The peek() method retrieves, but does not remove, the element at the top of the stack; if
the stack is empty, it returns null . Calling the peek() method is the same as calling the peekFirst() method. A stack
needs four methods to perform its operations: isEmpty() , push() , pop() and peek() . Table 12-4 lists the stack specific
methods in the Deque interface and their alternate versions.
Table 12-4. Deque Methods Named Specifically to be Used with Stacks
Stack Specific Methods in Deque
Equivalent Alternate Methods in Deque
isEmpty()
Inherited from the Collection interface
push(E e)
addFirst(E e)
pop()
removeFirst()
peek()
peekFirst()
Looking at the methods that you have seen so far in the Deque interface, you can say that it is a huge interface.
A programmer can easily get confused if he does not learn this interface by breaking its methods down into separate
categories. The Deque interface has methods that fall into the following four categories:
Deque ,
as listed in Table 12-2 . All these methods are enough to use a Deque as any queue you want.
However, it offers some more methods with different names to accomplish the same thing.
Methods that let you insert, remove, and peek elements at the head and tail of the
Deque as a FIFO queue (or simply as Queue ). They are listed in
Methods that let you use a
Table 12-3 .
Methods that let you use familiar method names that are used with stacks. Note that these
methods are not performing anything new other than insertion, removal, and peeking. They
just have different names. They are listed in Table 12-4 .
Deque in specific situations. For example,
its descendingIterator() method returns an Iterator object that lets you iterate
over its elements in a reverse order (from tail to head). It also adds two methods called
removeFirstOccurrence(Object o) and removeLastOccurrence(Object o) that let you
remove the first occurrence (starting from the head and going towards the tail) and last
occurrence (starting from the tail and going towards the head) of an object in the Deque ,
respectively. Now you can relax—there are no more new methods in the Deque to learn.
The ArrayDeque and LinkedList classes are two implementation classes for the Deque interface. The ArrayDeque
class is backed by an array whereas the LinkedList class is backed by a linked list. You should use the ArrayDeque
as a Deque implementation if you are using a Deque as a LIFO queue (or a stack). The LinkedList implementation
performs better if you use a Deque as a FIFO queue (or simply as a Queue ).
Listing 12-19 demonstrates how to use a Deque as a FIFO queue. If you compare this program with the program
in Listing 12-15, in this program you have just used Deque -specific methods to perform the same thing as what you
accomplished with the methods of the Queue interface. Suppose a method accepts an argument of type Queue . If you
pass a Deque to that method, your Deque will be used as a FIFO queue inside that method.
Some utility methods that help you work with a
 
 
Search WWH ::




Custom Search