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.
,
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
, SequencedCollection
, and Iterator
interfaces.
This class is a member of the Java Collections Framework.
Modifier and Type | Class and Description |
---|---|
private class | |
pack-priv class | |
private class |
Modifier and Type | Field and Description |
---|---|
pack-priv transient Object[] | elements
The array in which the elements of the deque are stored. |
pack-priv transient int | head
The index of the element at the head of the deque (which is the element that would be removed by remove() or pop()); or an arbitrary number 0 <= head < elements.length equal to tail if the deque is empty. |
private static final int | MAX_ARRAY_SIZE
The maximum size of array to allocate. |
private static final long | |
pack-priv transient int | tail
The index at which the next element would be added to the tail of the deque (via addLast(E), add(E), or push(E)); elements[tail] is always null. |
Access | Constructor and Description |
---|---|
public | ArrayDeque()
Constructs an empty array deque with an initial capacity sufficient to hold 16 elements. |
public | ArrayDeque(int
lower bound on initial capacity of the deque numElements)Constructs an empty array deque with an initial capacity sufficient to hold the specified number of elements. |
public | ArrayDeque(Collection<? extends E>
the collection whose elements are to be placed into the deque c)Constructs a deque containing the elements of the specified collection, in the order they are returned by the collection's iterator. |
Modifier and Type | Method and Description |
---|---|
public boolean | add(E
the element to add e)Overrides java. Implements java. Inserts the specified element at the end of this deque. |
public boolean | Returns: true if this deque changed as a result of the callthe elements to be inserted into this deque c)Overrides java. Implements java. Adds all of the elements in the specified collection at the end
of this deque, as if by calling |
public void | addFirst(E
the element to add e)Implements java. Inserts the specified element at the front of this deque. |
public void | addLast(E
the element to add e)Implements java. Inserts the specified element at the end of this deque. |
private boolean | |
private boolean | bulkRemoveModified(Predicate<? super E> filter, final int
valid index of first element to be deleted beg)Helper for bulkRemove, in case of at least one deletion. |
pack-priv void | |
private static void | circularClear(Object[] es, int i, int end)
Nulls out slots starting at array index i, up to index end. |
public void | clear()
Overrides java. Implements java. Removes all of the elements from this deque. |
public ArrayDeque | |
public boolean | Returns: true if this deque contains the specified elementobject to be checked for containment in this deque o)Overrides java. Implements java. Returns |
private void | |
pack-priv static final int | |
pack-priv boolean | Returns: true if elements near tail moved backwardsRemoves the element at the specified position in the elements array. |
public Iterator | descendingIterator()
Implements java. Returns an iterator over the elements in this deque in reverse sequential order. |
public E | Returns: the head of the queue represented by this dequeImplements java. Retrieves, but does not remove, the head of the queue represented by this deque. |
pack-priv static final <E> E | |
public void | forEach(Consumer<? super E>
The action to be performed for each element action)Overrides default java. Performs the given action for each element of the |
public E | getFirst()
Implements java. Retrieves, but does not remove, the first element of this deque. |
public E | getLast()
Implements java. Retrieves, but does not remove, the last element of this deque. |
private void | grow(int
the required minimum extra capacity; must be positive needed)Increases the capacity of this deque by at least the given amount. |
pack-priv static final int | |
pack-priv static final int | Returns: index 0 <= i < modulusCircularly adds the given distance to index i, mod modulus. |
private static boolean | |
public boolean | Returns: true if this deque contains no elementsOverrides java. Implements java. Returns |
public Iterator | Returns: an iterator over the elements in this dequeImplements abstract java. Implements java. Returns an iterator over the elements in this deque. |
private static long[] | |
private int | |
pack-priv static final <E> E | |
public boolean | offer(E
the element to add e)Implements java. Inserts the specified element at the end of this deque. |
public boolean | offerFirst(E
the element to add e)Implements java. Inserts the specified element at the front of this deque. |
public boolean | offerLast(E
the element to add e)Implements java. Inserts the specified element at the end of this deque. |
public E | Returns: the head of the queue represented by this deque, ornull if this deque is emptyImplements java. Retrieves, but does not remove, the head of the queue represented by
this deque, or returns |
public E | peekFirst()
Implements java. Retrieves, but does not remove, the first element of this deque,
or returns |
public E | peekLast()
Implements java. Retrieves, but does not remove, the last element of this deque,
or returns |
public E | Returns: the head of the queue represented by this deque, ornull if this deque is emptyImplements java. Retrieves and removes the head of the queue represented by this deque
(in other words, the first element of this deque), or returns
|
public E | pollFirst()
Implements java. Retrieves and removes the first element of this deque,
or returns |
public E | pollLast()
Implements java. Retrieves and removes the last element of this deque,
or returns |
public E | Returns: the element at the front of this deque (which is the top of the stack represented by this deque)Implements java. Pops an element from the stack represented by this deque. |
public void | push(E
the element to push e)Implements java. Pushes an element onto the stack represented by this deque. |
private void | readObject(ObjectInputStream
the stream s)Reconstitutes this deque from a stream (that is, deserializes it). |
public E | Returns: the head of the queue represented by this dequeImplements java. Retrieves and removes the head of the queue represented by this deque. |
public boolean | Returns: true if this deque contained the specified elementelement to be removed from this deque, if present o)Overrides java. Implements java. Removes a single instance of the specified element from this deque. |
public boolean | removeAll(Collection<?>
collection containing elements to be removed from this collection c)Overrides java. Implements java. Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
public E | removeFirst()
Implements java. Retrieves and removes the first element of this deque. |
public boolean | Returns: true if the deque contained the specified elementelement to be removed from this deque, if present o)Implements java. Removes the first occurrence of the specified element in this deque (when traversing the deque from head to tail). |
public boolean | removeIf(Predicate<? super E>
a predicate which returns filter)true for elements to be
removedOverrides default java. Removes all of the elements of this collection that satisfy the given predicate (optional operation). |
public E | removeLast()
Implements java. Retrieves and removes the last element of this deque. |
public boolean | Returns: true if the deque contained the specified elementelement to be removed from this deque, if present o)Implements java. Removes the last occurrence of the specified element in this deque (when traversing the deque from head to tail). |
public boolean | retainAll(Collection<?>
collection containing elements to be retained in this collection c)Overrides java. Implements java. Retains only the elements in this collection that are contained in the specified collection (optional operation). |
private static void | |
public int | Returns: the number of elements in this dequeImplements abstract java. Implements java. Returns the number of elements in this deque. |
public Spliterator | Returns: aSpliterator over the elements in this dequeOverrides default java. Creates a late-binding
and fail-fast |
pack-priv static final int | Returns: the "circular distance" from j to i; corner case i == j is disambiguated to "empty", returning 0.Subtracts j from i, mod modulus. |
public Object[] | Returns: an array containing all of the elements in this dequeOverrides java. Implements java. Returns an array containing all of the elements in this deque in proper sequence (from first to last element). |
private <T> T[] | |
public <T> T[] | Returns: an array containing all of the elements in this dequethe array into which the elements of the deque are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose a)Overrides java. Implements java. Returns an array containing all of the elements in this deque in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. |
private void |