Deque

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 MethodEquivalent 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 MethodEquivalent Deque Method
push(e)addFirst(e)
pop()removeFirst()
peek()peekFirst()

This interface is a member of the Java Collections Framework.

ReturnMethodSummary
booleanadd(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.
voidaddFirst(E e)Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions.
voidaddLast(E e)Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions.
booleancontains(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.
Eelement()Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque).
EgetFirst()Retrieves, but does not remove, the first element of this deque.
EgetLast()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.
booleanoffer(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.
booleanofferFirst(E e)Inserts the specified element at the front of this deque unless it would violate capacity restrictions.
booleanofferLast(E e)Inserts the specified element at the end of this deque unless it would violate capacity restrictions.
Epeek()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.
EpeekFirst()Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
EpeekLast()Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
Epoll()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.
EpollFirst()Retrieves and removes the first element of this deque, or returns null if this deque is empty.
EpollLast()Retrieves and removes the last element of this deque, or returns null if this deque is empty.
Epop()Pops an element from the stack represented by this deque.
voidpush(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.
Eremove()Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque).
booleanremove(Object o)Removes the first occurrence of the specified element from this deque.
EremoveFirst()Retrieves and removes the first element of this deque.
booleanremoveFirstOccurrence(Object o)Removes the first occurrence of the specified element from this deque.
EremoveLast()Retrieves and removes the last element of this deque.
booleanremoveLastOccurrence(Object o)Removes the last occurrence of the specified element from this deque.
intsize()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() + " ");
  }
}
  
Revised from Open JDK source code
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.