Java Reference
In-Depth Information
public
T removeLast()
Retrieves and removes the entry at the back of this deque, but throws
NoSuchElementException
if the deque is empty prior to the operation.
public
T pollLast()
Retrieves and removes the entry at the back of this deque, but returns
null
if the deque is
empty prior to the operation.
public
T getFirst()
Retrieves the entry at the front of this deque, but throws
NoSuchElementException
if the
deque is empty.
public
T peekFirst()
Retrieves the entry at the front of this deque, but returns
null
if the deque is empty.
public
T getLast()
Retrieves the entry at the back of this deque, but throws
NoSuchElementException
if the
deque is empty.
public
T peekLast()
Retrieves the entry at the back of this deque, but returns
null
if the deque is empty.
public boolean
isEmpty()
Detects whether this deque is empty.
public void
clear()
Removes all entries from this deque.
public int
size()
Gets the number of entries currently in this deque.
The interface
Deque
extends the interface
Queue
, so it also has the methods
add
,
offer
,
remove
,
poll
,
element
, and
peek
that were described earlier in Segment 10.13. In addition,
Deque
declares
the following two stack methods:
public void
push(T newEntry)
public
T pop()
These methods are like the ones defined in the class
java.util.Stack
, which we encountered ear-
lier in Segment 5.23 of Chapter 5, except that
push
is a
void
method in
Deque
. As we mentioned in
Chapter 5, you should no longer use the standard class
Stack
. The following segment describes an
alternate class for you to use.
The online documentation for the interface
Deque
, as given at
download.oracle.com/javase/7/
docs/api
, tabulates the correspondence between deque methods and both queue and stack methods.
The standard package
java.util
in the Java Class Library contains the class
ArrayDeque
, which
implements the interface
Deque
that we just described. Since
Deque
declares methods appropriate for a
deque, a queue, and a stack, you can use
ArrayDeque
to create instances of any of these data collections.