Top Description Inners Fields Constructors Methods
java.util

public Class ArrayDeque<E>

extends AbstractCollection<E>
implements Deque<E>, Cloneable, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Cloneable, java.util.Deque, java.util.SequencedCollection, java.util.Collection, java.lang.Iterable, java.util.Queue
Known Direct Subclasses
sun.net.www.http.KeepAliveCache.ClientVector
Type Parameters
<E>
the type of elements held in this deque
Imports
java.io.Serializable, java.util.function.Consumer, .Predicate, jdk.internal.access.SharedSecrets

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, SequencedCollection, and Iterator interfaces.

This class is a member of the Java Collections Framework.

Author
Josh Bloch and Doug Lea
Since
1.6

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
pack-priv class
private class

Field Summary

Modifier and TypeField 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.

Constructor Summary

AccessConstructor 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.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true (as specified by Collection#add)
add
(E
the element to add
e
)

Overrides java.util.AbstractCollection.add.

Implements java.util.Deque.add, java.util.Collection.add.

Inserts the specified element at the end of this deque.

public boolean

Returns:

true if this deque changed as a result of the call
addAll
(Collection<? extends E>
the elements to be inserted into this deque
c
)

Overrides java.util.AbstractCollection.addAll.

Implements java.util.Deque.addAll, java.util.Collection.addAll.

Adds all of the elements in the specified collection at the end of this deque, as if by calling addLast on each one, in the order that they are returned by the collection's iterator.

public void
addFirst(E
the element to add
e
)

Implements java.util.Deque.addFirst.

Inserts the specified element at the front of this deque.

public void
addLast(E
the element to add
e
)

Implements java.util.Deque.addLast.

Inserts the specified element at the end of this deque.

private boolean
bulkRemove(Predicate<? super E> filter)

Implementation of bulk remove methods.

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
checkInvariants()

debugging

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.util.AbstractCollection.clear.

Implements java.util.Collection.clear.

Removes all of the elements from this deque.

public ArrayDeque<E>

Returns:

a copy of this deque
clone
()

Overrides java.lang.Object.clone.

Returns a copy of this deque.

public boolean

Returns:

true if this deque contains the specified element
contains
(Object
object to be checked for containment in this deque
o
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.Deque.contains, java.util.Collection.contains.

Returns true if this deque contains the specified element.

private void
copyElements(Collection<? extends E> c)

pack-priv static final int
dec(int i, int modulus)

Circularly decrements i, mod modulus.

pack-priv boolean

Returns:

true if elements near tail moved backwards
delete
(int i)

Removes the element at the specified position in the elements array.

public Iterator<E>
descendingIterator()

Implements java.util.Deque.descendingIterator.

Returns an iterator over the elements in this deque in reverse sequential order.

public E

Returns:

the head of the queue represented by this deque
element
()

Implements java.util.Deque.element.

Retrieves, but does not remove, the head of the queue represented by this deque.

pack-priv static final <E> E
elementAt(Object[] es, int i)

Returns element at array index i.

public void
forEach(Consumer<? super E>
The action to be performed for each element
action
)

Overrides default java.lang.Iterable.forEach.

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

public E
getFirst()

Implements java.util.Deque.getFirst.

Retrieves, but does not remove, the first element of this deque.

public E
getLast()

Implements java.util.Deque.getLast.

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
inc(int i, int modulus)

Circularly increments i, mod modulus.

pack-priv static final int

Returns:

index 0 <= i < modulus
inc
(int i, int distance, int modulus)

Circularly adds the given distance to index i, mod modulus.

private static boolean
isClear(long[] bits, int i)

public boolean

Returns:

true if this deque contains no elements
isEmpty
()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Collection.isEmpty.

Returns true if this deque contains no elements.

public Iterator<E>

Returns:

an iterator over the elements in this deque
iterator
()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Deque.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this deque.

private static long[]
nBits(int n)

private int
newCapacity(int needed, int jump)

Capacity calculation for edge conditions, especially overflow.

pack-priv static final <E> E
nonNullElementAt(Object[] es, int i)

A version of elementAt that checks for null elements.

public boolean

Returns:

true (as specified by Queue#offer)
offer
(E
the element to add
e
)

Implements java.util.Deque.offer.

Inserts the specified element at the end of this deque.

public boolean

Returns:

true (as specified by Deque#offerFirst)
offerFirst
(E
the element to add
e
)

Implements java.util.Deque.offerFirst.

Inserts the specified element at the front of this deque.

public boolean

Returns:

true (as specified by Deque#offerLast)
offerLast
(E
the element to add
e
)

Implements java.util.Deque.offerLast.

Inserts the specified element at the end of this deque.

public E

Returns:

the head of the queue represented by this deque, or null if this deque is empty
peek
()

Implements java.util.Deque.peek.

Retrieves, but does not remove, the head of the queue represented by this deque, or returns null if this deque is empty.

public E
peekFirst()

Implements java.util.Deque.peekFirst.

Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.

public E
peekLast()

Implements java.util.Deque.peekLast.

Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.

public E

Returns:

the head of the queue represented by this deque, or null if this deque is empty
poll
()

Implements java.util.Deque.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.

public E
pollFirst()

Implements java.util.Deque.pollFirst.

Retrieves and removes the first element of this deque, or returns null if this deque is empty.

public E
pollLast()

Implements java.util.Deque.pollLast.

Retrieves and removes the last element of this deque, or returns null if this deque is empty.

public E

Returns:

the element at the front of this deque (which is the top of the stack represented by this deque)
pop
()

Implements java.util.Deque.pop.

Pops an element from the stack represented by this deque.

public void
push(E
the element to push
e
)

Implements java.util.Deque.push.

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 deque
remove
()

Implements java.util.Deque.remove.

Retrieves and removes the head of the queue represented by this deque.

public boolean

Returns:

true if this deque contained the specified element
remove
(Object
element to be removed from this deque, if present
o
)

Overrides java.util.AbstractCollection.remove.

Implements java.util.Deque.remove, java.util.Collection.remove.

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.util.AbstractCollection.removeAll.

Implements java.util.Collection.removeAll.

Removes all of this collection's elements that are also contained in the specified collection (optional operation).

public E
removeFirst()

Implements java.util.Deque.removeFirst.

Retrieves and removes the first element of this deque.

public boolean

Returns:

true if the deque contained the specified element
removeFirstOccurrence
(Object
element to be removed from this deque, if present
o
)

Implements java.util.Deque.removeFirstOccurrence.

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 true for elements to be removed
filter
)

Overrides default java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate (optional operation).

public E
removeLast()

Implements java.util.Deque.removeLast.

Retrieves and removes the last element of this deque.

public boolean

Returns:

true if the deque contained the specified element
removeLastOccurrence
(Object
element to be removed from this deque, if present
o
)

Implements java.util.Deque.removeLastOccurrence.

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.util.AbstractCollection.retainAll.

Implements java.util.Collection.retainAll.

Retains only the elements in this collection that are contained in the specified collection (optional operation).

private static void
setBit(long[] bits, int i)

public int

Returns:

the number of elements in this deque
size
()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Deque.size, java.util.Collection.size.

Returns the number of elements in this deque.

public Spliterator<E>

Returns:

a Spliterator over the elements in this deque
spliterator
()

Overrides default java.util.Collection.spliterator.

Creates a late-binding and fail-fast Spliterator over the elements in this deque.

pack-priv static final int

Returns:

the "circular distance" from j to i; corner case i == j is disambiguated to "empty", returning 0.
sub
(int i, int j, int modulus)

Subtracts j from i, mod modulus.

public Object[]

Returns:

an array containing all of the elements in this deque
toArray
()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this deque in proper sequence (from first to last element).

private <T> T[]
toArray(Class<T[]> klazz)

public <T> T[]

Returns:

an array containing all of the elements in this deque
toArray
(T[]
the 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.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

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
writeObject(ObjectOutputStream
the stream
s
)

Saves this deque to a stream (that is, serializes it).

Inherited from java.util.AbstractCollection:
containsAlltoString