class ArrayDeque[E] extends AbstractCollection[E] with Deque[E] with Cloneable with 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 java.util.Stack when used as a stack,
and faster than LinkedList
when used as a queue.
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 <a href="/java.base/java/util/package-summary.html#CollectionsFramework"> Java Collections Framework.
- Since
1.6
- Alphabetic
- By Inheritance
- ArrayDeque
- Serializable
- Cloneable
- Deque
- SequencedCollection
- Queue
- AbstractCollection
- Collection
- Iterable
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
- new ArrayDeque(c: Collection[_ <: E])
Constructs a deque containing the elements of the specified collection, in the order they are returned by the collection's iterator.
Constructs a deque containing the elements of the specified collection, in the order they are returned by the collection's iterator. (The first element returned by the collection's iterator becomes the first element, or front of the deque.)
- c
the collection whose elements are to be placed into the deque
- Exceptions thrown
java.lang.NullPointerException
if the specified collection is null
- new ArrayDeque(numElements: Int)
Constructs an empty array deque with an initial capacity sufficient to hold the specified number of elements.
Constructs an empty array deque with an initial capacity sufficient to hold the specified number of elements.
- numElements
lower bound on initial capacity of the deque
- new ArrayDeque()
Constructs an empty array deque with an initial capacity sufficient to hold 16 elements.
- new ArrayDeque(elements: Array[AnyRef])
Type Members
- final class DeqSpliterator extends Spliterator[E]
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def add(e: E): Boolean
Inserts the specified element at the end of this deque.
Inserts the specified element at the end of this deque.
This method is equivalent to
#addLast
.- e
the element to add
- returns
true
(as specified byCollection#add
)
- Definition Classes
- ArrayDeque → Deque → Queue → AbstractCollection → Collection
- Exceptions thrown
java.lang.NullPointerException
if the specified element is null
- def addAll(c: Collection[_ <: E]): Boolean
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.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.- c
the elements to be inserted into this deque
- returns
true
if this deque changed as a result of the call
- Definition Classes
- ArrayDeque → AbstractCollection → Collection
- Exceptions thrown
java.lang.NullPointerException
if the specified collection or any of its elements are null
- def addFirst(e: E): Unit
Inserts the specified element at the front of this deque.
Inserts the specified element at the front of this deque.
- e
the element to add
- Definition Classes
- ArrayDeque → Deque
- Exceptions thrown
java.lang.NullPointerException
if the specified element is null
- def addLast(e: E): Unit
Inserts the specified element at the end of this deque.
Inserts the specified element at the end of this deque.
This method is equivalent to
#add
.- e
the element to add
- Definition Classes
- ArrayDeque → Deque
- Exceptions thrown
java.lang.NullPointerException
if the specified element is null
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def bulkRemove(filter: Predicate[_ >: E]): Boolean
Implementation of bulk remove methods.
- def clear(): Unit
Removes all of the elements from this deque.
Removes all of the elements from this deque. The deque will be empty after this call returns.
- Definition Classes
- ArrayDeque → AbstractCollection → Collection
- def clone(): ArrayDeque[E]
Returns a copy of this deque.
- def contains(o: Any): Boolean
Returns
true
if this deque contains the specified element.Returns
true
if this deque contains the specified element. More formally, returnstrue
if and only if this deque contains at least one elemente
such thato.equals(e)
.- o
object to be checked for containment in this deque
- returns
true
if this deque contains the specified element
- Definition Classes
- ArrayDeque → Deque → AbstractCollection → Collection
- def containsAll(c: Collection[_]): Boolean
- Definition Classes
- AbstractCollection → Collection
- def descendingIterator(): Iterator[E]
- Definition Classes
- ArrayDeque → Deque
- def element(): E
Retrieves, but does not remove, the head of the queue represented by this deque.
Retrieves, but does not remove, the head of the queue represented by this deque. This method differs from
peek
only in that it throws an exception if this deque is empty.This method is equivalent to
#getFirst
.- returns
the head of the queue represented by this deque
- Definition Classes
- ArrayDeque → Deque → Queue
- Exceptions thrown
- var elements: Array[AnyRef]
- def ensureCapacity(minCapacity: Int): Unit
Increases the internal storage of this collection, if necessary, to ensure that it can hold at least the given number of elements.
Increases the internal storage of this collection, if necessary, to ensure that it can hold at least the given number of elements.
- minCapacity
the desired minimum capacity
- Since
TBD
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- def forEach(action: Consumer[_ >: E]): Unit
- Definition Classes
- ArrayDeque → Iterable
- Exceptions thrown
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def getFirst(): E
- Definition Classes
- ArrayDeque → Deque
- Exceptions thrown
- def getLast(): E
- Definition Classes
- ArrayDeque → Deque
- Exceptions thrown
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- var head: Int
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.
- def isEmpty(): Boolean
Returns
true
if this deque contains no elements.Returns
true
if this deque contains no elements.- returns
true
if this deque contains no elements
- Definition Classes
- ArrayDeque → AbstractCollection → Collection
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def iterator(): Iterator[E]
Returns an iterator over the elements in this deque.
Returns an iterator over the elements in this deque. The elements will be ordered from first (head) to last (tail). This is the same order that elements would be dequeued (via successive calls to remove or popped (via successive calls to
#pop
).- returns
an iterator over the elements in this deque
- Definition Classes
- ArrayDeque → Deque → AbstractCollection → Collection → Iterable
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def offer(e: E): Boolean
Inserts the specified element at the end of this deque.
Inserts the specified element at the end of this deque.
This method is equivalent to
#offerLast
.- e
the element to add
- returns
true
(as specified byQueue#offer
)
- Definition Classes
- ArrayDeque → Deque → Queue
- Exceptions thrown
java.lang.NullPointerException
if the specified element is null
- def offerFirst(e: E): Boolean
Inserts the specified element at the front of this deque.
Inserts the specified element at the front of this deque.
- e
the element to add
- returns
true
(as specified byDeque#offerFirst
)
- Definition Classes
- ArrayDeque → Deque
- Exceptions thrown
java.lang.NullPointerException
if the specified element is null
- def offerLast(e: E): Boolean
Inserts the specified element at the end of this deque.
Inserts the specified element at the end of this deque.
- e
the element to add
- returns
true
(as specified byDeque#offerLast
)
- Definition Classes
- ArrayDeque → Deque
- Exceptions thrown
java.lang.NullPointerException
if the specified element is null
- def parallelStream(): Stream[E]
- Definition Classes
- Collection
- def peek(): E
Retrieves, but does not remove, the head of the queue represented by this deque, or returns
null
if this deque is empty.Retrieves, but does not remove, the head of the queue represented by this deque, or returns
null
if this deque is empty.This method is equivalent to
#peekFirst
.- returns
the head of the queue represented by this deque, or
null
if this deque is empty
- Definition Classes
- ArrayDeque → Deque → Queue
- def peekFirst(): E
- Definition Classes
- ArrayDeque → Deque
- def peekLast(): E
- Definition Classes
- ArrayDeque → Deque
- def 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.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.This method is equivalent to
#pollFirst
.- returns
the head of the queue represented by this deque, or
null
if this deque is empty
- Definition Classes
- ArrayDeque → Deque → Queue
- def pollFirst(): E
- Definition Classes
- ArrayDeque → Deque
- def pollLast(): E
- Definition Classes
- ArrayDeque → Deque
- def pop(): E
Pops an element from the stack represented by this deque.
Pops an element from the stack represented by this deque. In other words, removes and returns the first element of this deque.
This method is equivalent to
#removeFirst
.- returns
the element at the front of this deque (which is the top of the stack represented by this deque)
- Definition Classes
- ArrayDeque → Deque
- Exceptions thrown
- def push(e: E): Unit
Pushes an element onto the stack represented by this deque.
Pushes an element onto the stack represented by this deque. In other words, inserts the element at the front of this deque.
This method is equivalent to
#addFirst
.- e
the element to push
- Definition Classes
- ArrayDeque → Deque
- Exceptions thrown
java.lang.NullPointerException
if the specified element is null
- def remove(o: Any): Boolean
Removes a single instance of the specified element from this deque.
Removes a single instance of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element
e
such thato.equals(e)
(if such an element exists). Returnstrue
if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).This method is equivalent to removeFirstOccurrence.
- o
element to be removed from this deque, if present
- returns
true
if this deque contained the specified element
- Definition Classes
- ArrayDeque → Deque → AbstractCollection → Collection
- def remove(): E
Retrieves and removes the head of the queue represented by this deque.
Retrieves and removes the head of the queue represented by this deque.
This method differs from
poll()
only in that it throws an exception if this deque is empty.This method is equivalent to
#removeFirst
.- returns
the head of the queue represented by this deque
- Definition Classes
- ArrayDeque → Deque → Queue
- Exceptions thrown
- def removeAll(c: Collection[_]): Boolean
- Definition Classes
- ArrayDeque → AbstractCollection → Collection
- Exceptions thrown
- def removeFirst(): E
- Definition Classes
- ArrayDeque → Deque
- Exceptions thrown
- def removeFirstOccurrence(o: Any): Boolean
Removes the first occurrence of the specified element in this deque (when traversing the deque from head to tail).
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 thato.equals(e)
(if such an element exists). Returnstrue
if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).- o
element to be removed from this deque, if present
- returns
true
if the deque contained the specified element
- Definition Classes
- ArrayDeque → Deque
- def removeIf(filter: Predicate[_ >: E]): Boolean
- Definition Classes
- ArrayDeque → Collection
- Exceptions thrown
- def removeLast(): E
- Definition Classes
- ArrayDeque → Deque
- Exceptions thrown
- def removeLastOccurrence(o: Any): Boolean
Removes the last occurrence of the specified element in this deque (when traversing the deque from head to tail).
Removes the last 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 last element such that
o.equals(e)
(if such an element exists). Returnstrue
if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).- o
element to be removed from this deque, if present
- returns
true
if the deque contained the specified element
- Definition Classes
- ArrayDeque → Deque
- def replaceAll(operator: UnaryOperator[E]): Unit
Replaces each element of this deque with the result of applying the operator to that element, as specified by
List#replaceAll
.Replaces each element of this deque with the result of applying the operator to that element, as specified by
List#replaceAll
.- operator
the operator to apply to each element
- Since
TBD
- def retainAll(c: Collection[_]): Boolean
- Definition Classes
- ArrayDeque → AbstractCollection → Collection
- Exceptions thrown
- def size(): Int
Returns the number of elements in this deque.
Returns the number of elements in this deque.
- returns
the number of elements in this deque
- Definition Classes
- ArrayDeque → Deque → AbstractCollection → Collection
- def spliterator(): Spliterator[E]
Creates a late-binding and fail-fast
Spliterator
over the elements in this deque.Creates a late-binding and fail-fast
Spliterator
over the elements in this deque.The
Spliterator
reports Spliterator.SIZED, Spliterator.SUBSIZED, Spliterator.ORDERED, and Spliterator.NONNULL. Overriding implementations should document the reporting of additional characteristic values.- returns
a
Spliterator
over the elements in this deque
- Definition Classes
- ArrayDeque → Collection → Iterable
- Since
1.8
- def stream(): Stream[E]
- Definition Classes
- Collection
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- var tail: Int
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.
- def toArray[T <: AnyRef](a: Array[T]): Array[T]
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.
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. If the deque fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this deque.
If this deque fits in the specified array with room to spare (i.e., the array has more elements than this deque), the element in the array immediately following the end of the deque is set to
null
.Like the toArray()* method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.
Suppose
x
is a deque known to contain only strings. The following code can be used to dump the deque into a newly allocated array ofString
:String[] y = x.toArray(new String[0]);
Note that
toArray(new Object[0])
is identical in function totoArray()
.- a
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
- returns
an array containing all of the elements in this deque
- Definition Classes
- ArrayDeque → AbstractCollection → Collection
- Exceptions thrown
java.lang.ArrayStoreException
if the runtime type of the specified array is not a supertype of the runtime type of every element in this dequejava.lang.NullPointerException
if the specified array is null
- def toArray(): Array[AnyRef]
Returns an array containing all of the elements in this deque in proper sequence (from first to last element).
Returns an array containing all of the elements in this deque in proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are maintained by this deque. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
- returns
an array containing all of the elements in this deque
- Definition Classes
- ArrayDeque → AbstractCollection → Collection
- def toString(): String
- Definition Classes
- AbstractCollection → AnyRef → Any
- def trimToSize(): Unit
Minimizes the internal storage of this collection.
Minimizes the internal storage of this collection.
- Since
TBD
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()