public interface

Deque

implements Queue<E>
java.util.Deque<E>
Known Indirect Subclasses

Class Overview

A kind of collection that can insert or remove element at both ends("double ended queue"). Mostly a deque has no limit of its size.

Extending from Queue, a deque can be used as a Queue which behavior is first-in-first-out. Furthermore, a deque can also be used as a Stack(legacy class) which behavior is last-in-first-out.

A typical deque does not allow null to be inserted as its element, while some implementations allow it. But null should not be inserted even in these implementations, since method poll return null to indicate that there is no element left in the deque.

A deque can also remove interior elements by removeFirstOccurrence and removeLastOccurrence methods. A deque can not access elements by index.

Summary

Public Methods
abstract void addFirst(E e)
Inserts an element at the head of this deque if it dose not violate size limit immediately.
abstract void addLast(E e)
Inserts an element at the tail of this deque if it dose not violate size limit immediately.
abstract Iterator<E> descendingIterator()
Returns the iterator in reverse order, from tail to head.
abstract E getFirst()
Gets but not removes the head element of this deque.
abstract E getLast()
Gets but not removes the tail element of this deque.
abstract boolean offerFirst(E e)
Inserts an element at the head of this deque unless it would violate size limit.
abstract boolean offerLast(E e)
Inserts an element at the tail of this deque unless it would violate size limit.
abstract E peekFirst()
Gets but not removes the head element of this deque.
abstract E peekLast()
Gets but not removes the tail element of this deque.
abstract E pollFirst()
Gets and removes the head element of this deque.
abstract E pollLast()
Gets and removes the tail element of this deque.
abstract E pop()
Pops the head element of the deque, just same as removeFirst().
abstract void push(E e)
Pushes the element to the deque(at the head of the deque), just same as addFirst(E).
abstract E removeFirst()
Gets and removes the head element of this deque.
abstract boolean removeFirstOccurrence(Object o)
Removes the first equivalent element of the specified object.
abstract E removeLast()
Gets and removes the tail element of this deque.
abstract boolean removeLastOccurrence(Object o)
Removes the last equivalent element of the specified object.
[Expand]
Inherited Methods
From interface java.lang.Iterable
From interface java.util.Collection
From interface java.util.Queue

Public Methods

public abstract void addFirst (E e)

Since: API Level 9

Inserts an element at the head of this deque if it dose not violate size limit immediately. It is better to use offerFirst(E) if a deque is size-limited.

Parameters
e the element
Throws
IllegalStateException if it can not add now due to size limit
ClassCastException if the class of element can not be added into this deque
NullPointerException if the element is null and the deque can not contain null element
IllegalArgumentException if the element can not be added due to some property.

public abstract void addLast (E e)

Since: API Level 9

Inserts an element at the tail of this deque if it dose not violate size limit immediately. It is better to use offerLast(E) if a deque is size-limited.

Parameters
e the element
Throws
IllegalStateException if it can not add now due to size limit
ClassCastException if the class of element can not be added into this deque
NullPointerException if the element is null and the deque can not contain null element
IllegalArgumentException if the element can not be added due to some property.

public abstract Iterator<E> descendingIterator ()

Since: API Level 9

Returns the iterator in reverse order, from tail to head.

Returns
  • the iterator in reverse order

public abstract E getFirst ()

Since: API Level 9

Gets but not removes the head element of this deque. This method throws an exception if the deque is empty.

Returns
  • the head element
Throws
NoSuchElementException if the deque is empty

public abstract E getLast ()

Since: API Level 9

Gets but not removes the tail element of this deque. This method throws an exception if the deque is empty.

Returns
  • the tail element
Throws
NoSuchElementException if the deque is empty

public abstract boolean offerFirst (E e)

Since: API Level 9

Inserts an element at the head of this deque unless it would violate size limit. It is better than the addFirst(E) method in a size-limited deque, because the latter one may fail to add the element only by throwing an exception.

Parameters
e the element
Returns
  • true if the operation succeeds or false if it fails.
Throws
ClassCastException if the class of element can not be added into this deque
NullPointerException if the element is null and the deque can not contain null element
IllegalArgumentException if the element can not be added due to some property.

public abstract boolean offerLast (E e)

Since: API Level 9

Inserts an element at the tail of this deque unless it would violate size limit. It is better than the addLast(E) method in a size-limited deque, because the latter one may fail to add the element only by throwing an exception.

Parameters
e the element
Returns
  • true if the operation succeeds or false if it fails
Throws
ClassCastException if the class of element can not be added into this deque
NullPointerException if the element is null and the deque can not contain null element
IllegalArgumentException if the element can not be added due to some property

public abstract E peekFirst ()

Since: API Level 9

Gets but not removes the head element of this deque. This method returns null if the deque is empty.

Returns
  • the head element or null if the deque is empty

public abstract E peekLast ()

Since: API Level 9

Gets but not removes the tail element of this deque. This method returns null if the deque is empty.

Returns
  • the tail element or null if the deque is empty

public abstract E pollFirst ()

Since: API Level 9

Gets and removes the head element of this deque. This method returns null if the deque is empty.

Returns
  • the head element or null if the deque is empty

public abstract E pollLast ()

Since: API Level 9

Gets and removes the tail element of this deque. This method returns null if the deque is empty.

Returns
  • the tail element or null if the deque is empty

public abstract E pop ()

Since: API Level 9

Pops the head element of the deque, just same as removeFirst().

Returns
  • the head element
Throws
NoSuchElementException if the deque is empty

public abstract void push (E e)

Since: API Level 9

Pushes the element to the deque(at the head of the deque), just same as addFirst(E).

Parameters
e the element
Throws
IllegalStateException if it can not add now due to size limit
ClassCastException if the class of element can not be added into this deque
NullPointerException if the element is null and the deque can not contain null element
IllegalArgumentException if the element can not be added due to some property.

public abstract E removeFirst ()

Since: API Level 9

Gets and removes the head element of this deque. This method throws an exception if the deque is empty.

Returns
  • the head element
Throws
NoSuchElementException if the deque is empty

public abstract boolean removeFirstOccurrence (Object o)

Since: API Level 9

Removes the first equivalent element of the specified object. If the deque does not contain the element, it is unchanged and returns false.

Parameters
o the element to be removed
Returns
  • true if the operation succeeds or false if the deque does not contain the element.
Throws
ClassCastException if the class of the element is incompatible with the deque
NullPointerException if the element is null and the deque can not contain null element

public abstract E removeLast ()

Since: API Level 9

Gets and removes the tail element of this deque. This method throws an exception if the deque is empty.

Returns
  • the tail element
Throws
NoSuchElementException if the deque is empty

public abstract boolean removeLastOccurrence (Object o)

Since: API Level 9

Removes the last equivalent element of the specified object. If the deque does not contain the element, it is unchanged and returns false.

Parameters
o the element to be removed
Returns
  • true if the operation succeeds or false if the deque does not contain the element.
Throws
ClassCastException if the class of the element is incompatible with the deque
NullPointerException if the element is null and the deque can not contain null element