w3resource

Java.util.ArrayDeque Class

ArrayDeque Class Method

public class ArrayDeque<E>

extends AbstractCollection<E>

implements Deque<E>, Cloneable, Serializable

Resizable-array implementation of the Deque interface. Array deques have no capacity restrictions; they grow as necessary to support usage. They are not thread-safe; in the absence of external synchronization, they do not support concurrent access by multiple threads. Null elements are prohibited. This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.

Most ArrayDeque operations run in amortized constant time.

Exceptions include remove, removeFirstOccurrence, removeLastOccurrence, contains, iterator.remove(), and the bulk operations, all of which run in linear time.

The iterators returned by this class's iterator method are fail-fast: If the deque is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will generally throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces.

This class is a member of the Java Collections Framework.

Method Summary:

Name Type Description
add(E e) boolean Inserts the specified element at the end of this deque.
addFirst void Inserts the specified element at the front of this deque.
addLast void Inserts the specified element at the end of this deque.
clear void Removes all of the elements from this deque.
clone ArrayDeque<E> Returns a copy of this deque.
contains boolean Returns true if this deque contains the specified element.
descendingIterator Iterator<E> Returns an iterator over the elements in this deque in reverse sequential order.
element() E Retrieves, but does not remove, the head of the queue represented by this deque.
getFirst() E Retrieves, but does not remove, the first element of this deque.
getLast(T[] a) E Retrieves, but does not remove, the last element of this deque.
isEmpty) boolean Returns true if this deque contains no elements.
iterator Iterator<E> Returns an iterator over the elements in this deque.
set boolean Inserts the specified element at the end of this deque.
offerFirst boolean Inserts the specified element at the front of this deque.
offerLast boolean Inserts the specified element at the end of this deque.
peek E Retrieves, but does not remove, the head of the queue represented by this deque, or returns null if this deque is empty.
peekFirst() E Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
peekLast E Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
poll E 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.
pollFirst E Retrieves and removes the first element of this deque, or returns null if this deque is empty.
pollLast E Retrieves and removes the last element of this deque, or returns null if this deque is empty.
pop E Pops an element from the stack represented by this deque. In other words,removes and returns the first element of this deque.
push void Pushes an element onto the stack represented by this deque. In other words, inserts the element at the front of this deque.
remove E Retrieves and removes the head of the queue represented by this deque.
removeFirst E Retrieves and removes the first element of this deque.
arraydeque_removeFirstOccurrence boolean Removes the first occurrence of the specified element in this deque (when traversing the deque from head to tail). If the deque does not contain the element, it is unchanged. More formally, removes the first element e such that o.equals(e) (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).
removeLast E Retrieves and removes the last element of this deque. This method differs from pollLast only in that it throws an exception if this deque is empty.
removeLastOccurrence() boolean Removes the last occurrence of the specified element in this deque (when traversing the deque from head to tail).
size int Returns the number of elements in this deque.
spliterator Spliterator<E> Creates a late-binding and fail-fast Spliterator over the elements in this deque.
arraydeque_toArray Object[] Returns an array containing all of the elements in this deque in proper sequence (from first to last element).

Previous: toString Method
Next: add Method



Share this Tutorial / Exercise on : Facebook and Twitter