A linear collection that supports element insertion and removal at both ends.
The twelve methods described above are summarized in the following table:
This interface extends the Queue interface. When a deque is used as a queue, FIFO (First-In-First-Out) behavior results.
Elements are added at the end of the deque and removed from the beginning.
The methods inherited from the Queue interface are precisely equivalent to Deque methods as indicated in the following table:
Queue Method | Equivalent Deque Method |
---|---|
add(e) | addLast(e) |
offer(e) | offerLast(e) |
remove() | removeFirst() |
poll() | pollFirst() |
element() | getFirst() |
peek() | peekFirst() |
Deques can also be used as LIFO (Last-In-First-Out) stacks. This interface should be used in preference to the legacy Stack class.
When a deque is used as a stack, elements are pushed and popped from the beginning of the deque. Stack methods are precisely equivalent to Deque methods as indicated in the table below:
Stack Method | Equivalent Deque Method |
---|---|
push(e) | addFirst(e) |
pop() | removeFirst() |
peek() | peekFirst() |
This interface is a member of the Java Collections Framework.
Return | Method | Summary |
---|---|---|
boolean | add(E e) | Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. |
void | addFirst(E e) | Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions. |
void | addLast(E e) | Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions. |
boolean | contains(Object o) | Returns true if this deque contains the specified element. |
Iterator<E> | descendingIterator() | Returns an iterator over the elements in this deque in reverse sequential order. |
E | element() | Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque). |
E | getFirst() | Retrieves, but does not remove, the first element of this deque. |
E | getLast() | Retrieves, but does not remove, the last element of this deque. |
Iterator<E> | iterator() | Returns an iterator over the elements in this deque in proper sequence. |
boolean | offer(E e) | Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available. |
boolean | offerFirst(E e) | Inserts the specified element at the front of this deque unless it would violate capacity restrictions. |
boolean | offerLast(E e) | Inserts the specified element at the end of this deque unless it would violate capacity restrictions. |
E | peek() | Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty. |
E | peekFirst() | Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty. |
E | peekLast() | Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty. |
E | poll() | Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty. |
E | pollFirst() | Retrieves and removes the first element of this deque, or returns null if this deque is empty. |
E | pollLast() | Retrieves and removes the last element of this deque, or returns null if this deque is empty. |
E | pop() | Pops an element from the stack represented by this deque. |
void | push(E e) | Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. |
E | remove() | Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque). |
boolean | remove(Object o) | Removes the first occurrence of the specified element from this deque. |
E | removeFirst() | Retrieves and removes the first element of this deque. |
boolean | removeFirstOccurrence(Object o) | Removes the first occurrence of the specified element from this deque. |
E | removeLast() | Retrieves and removes the last element of this deque. |
boolean | removeLastOccurrence(Object o) | Removes the last occurrence of the specified element from this deque. |
int | size() | Returns the number of elements in this deque. |
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String args[]) {
Deque<String> stack = new ArrayDeque<String>();
Deque<String> queue = new ArrayDeque<String>();
stack.push("A");
stack.push("B");
stack.push("C");
stack.push("java2s.com");
while (!stack.isEmpty())
System.out.print(stack.pop() + " ");
queue.add("A");
queue.add("B");
queue.add("C");
queue.add("D");
while (!queue.isEmpty())
System.out.print(queue.remove() + " ");
}
}
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |