ArrayDeque
Defined in: src/queue/ArrayDeque.ts:64
Resizable circular-array (ring buffer) Deque: the general-purpose queue, deque and stack.
Every operation at either end is O(1) (adds are amortized), with no per-element allocation.
contains, remove(o) and the remove*Occurrence methods are O(n); removing an inner element
shifts whichever side is shorter. Rejects null and undefined with NullPointerException,
so a null from poll() or peek() always means empty. Iterates head to tail; iterators and
forEach are fail-fast.
The buffer length is a power of two, at least 8 and 16 by default; one slot always stays free.
When it fills, it doubles in place, moving the shorter wrapped segment. It never shrinks:
clear() keeps the capacity.
Extends
Section titled “Extends”Type Parameters
Section titled “Type Parameters”E
Implements
Section titled “Implements”Deque<E>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ArrayDeque<
E>():ArrayDeque<E>
Defined in: src/queue/ArrayDeque.ts:72
Creates an empty deque with the default 16-slot buffer.
Returns
Section titled “Returns”ArrayDeque<E>
Overrides
Section titled “Overrides”AbstractCollection.constructor
Constructor
Section titled “Constructor”new ArrayDeque<
E>(numElements):ArrayDeque<E>
Defined in: src/queue/ArrayDeque.ts:79
Creates an empty deque sized for numElements: the buffer is the smallest power of two greater
than numElements (at least 8, at most 2^30). It is only a hint: a negative or non-integer value
is not rejected, and the deque still grows past it.
Parameters
Section titled “Parameters”numElements
Section titled “numElements”number
expected number of elements
Returns
Section titled “Returns”ArrayDeque<E>
Overrides
Section titled “Overrides”AbstractCollection<E>.constructor
Constructor
Section titled “Constructor”new ArrayDeque<
E>(c):ArrayDeque<E>
Defined in: src/queue/ArrayDeque.ts:84
Creates a deque holding the elements of c, in its iteration order (head first). O(n).
Parameters
Section titled “Parameters”Collection<E>
Returns
Section titled “Returns”ArrayDeque<E>
Throws
Section titled “Throws”NullPointerException if c is null or undefined, or holds null or undefined
Overrides
Section titled “Overrides”AbstractCollection<E>.constructor
Methods
Section titled “Methods”[INSPECT]()
Section titled “[INSPECT]()”[INSPECT]():
string
Defined in: src/abstract/AbstractCollection.ts:195
Node’s util.inspect hook: console.log prints the toString() form.
Returns
Section titled “Returns”string
Inherited from
Section titled “Inherited from”[iterator]()
Section titled “[iterator]()”[iterator]():
IterableIterator<E>
Defined in: src/queue/ArrayDeque.ts:484
Returns a JS iterator over the elements, head to tail, for for...of and spread; fail-fast.
Returns
Section titled “Returns”IterableIterator<E>
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”add(
e):boolean
Defined in: src/queue/ArrayDeque.ts:182
Inserts e at the end; same as addLast(e). Amortized O(1).
Parameters
Section titled “Parameters”E
Returns
Section titled “Returns”boolean
always true
Throws
Section titled “Throws”NullPointerException if e is null or undefined
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”addAll()
Section titled “addAll()”addAll(
c):boolean
Defined in: src/queue/ArrayDeque.ts:211
Appends every element of c at the end, in c’s iteration order. Every element is checked first,
so a rejected element leaves the deque unchanged.
Parameters
Section titled “Parameters”Collection<E>
Returns
Section titled “Returns”boolean
true if this deque changed (c was not empty)
Throws
Section titled “Throws”NullPointerException if c is null, or holds null or undefined
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”addFirst()
Section titled “addFirst()”addFirst(
e):void
Defined in: src/queue/ArrayDeque.ts:135
Inserts e at the front, in amortized O(1).
Parameters
Section titled “Parameters”E
Returns
Section titled “Returns”void
Throws
Section titled “Throws”NullPointerException if e is null or undefined
Implementation of
Section titled “Implementation of”addLast()
Section titled “addLast()”addLast(
e):void
Defined in: src/queue/ArrayDeque.ts:148
Inserts e at the end, in amortized O(1).
Parameters
Section titled “Parameters”E
Returns
Section titled “Returns”void
Throws
Section titled “Throws”NullPointerException if e is null or undefined
Implementation of
Section titled “Implementation of”clear()
Section titled “clear()”clear():
void
Defined in: src/queue/ArrayDeque.ts:355
Removes all elements. O(n); keeps the current capacity, since the buffer never shrinks.
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”clone()
Section titled “clone()”clone():
ArrayDeque<E>
Defined in: src/queue/ArrayDeque.ts:109
Returns a shallow, independent copy of the same runtime class (same capacity), in O(capacity).
Returns
Section titled “Returns”ArrayDeque<E>
contains()
Section titled “contains()”contains(
o):boolean
Defined in: src/queue/ArrayDeque.ts:411
Returns true if this deque contains an element equal to o (by Objects.equals). O(n); false for null or undefined.
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”boolean
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”containsAll()
Section titled “containsAll()”containsAll(
c):boolean
Defined in: src/abstract/AbstractCollection.ts:83
Returns true if this collection contains every element of c (calls contains for each).
Parameters
Section titled “Parameters”Collection<unknown>
Returns
Section titled “Returns”boolean
Throws
Section titled “Throws”NullPointerException if c is null
Implementation of
Section titled “Implementation of”Inherited from
Section titled “Inherited from”AbstractCollection.containsAll
descendingIterator()
Section titled “descendingIterator()”descendingIterator():
Iterator<E>
Defined in: src/queue/ArrayDeque.ts:470
Returns a fail-fast iterator over the elements from last to first. Its remove is O(n), shifting the shorter side.
Returns
Section titled “Returns”Iterator<E>
Implementation of
Section titled “Implementation of”element()
Section titled “element()”element():
E
Defined in: src/queue/ArrayDeque.ts:406
Returns the head without removing it, in O(1); same as getFirst().
Returns
Section titled “Returns”E
Throws
Section titled “Throws”NoSuchElementException if the deque is empty
Implementation of
Section titled “Implementation of”equals()
Section titled “equals()”equals(
o):boolean
Defined in: src/abstract/AbstractCollection.ts:185
Identity by default; List/Set/Map subclasses override with value equality.
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”boolean
Implementation of
Section titled “Implementation of”Inherited from
Section titled “Inherited from”forEach()
Section titled “forEach()”forEach(
action):void
Defined in: src/queue/ArrayDeque.ts:440
Calls action for each element, head to tail.
Parameters
Section titled “Parameters”action
Section titled “action”(e) => void
Returns
Section titled “Returns”void
Throws
Section titled “Throws”NullPointerException if action is null
Throws
Section titled “Throws”ConcurrentModificationException if action structurally modifies this deque
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”getFirst()
Section titled “getFirst()”getFirst():
E
Defined in: src/queue/ArrayDeque.ts:387
Returns the first element. O(1).
Returns
Section titled “Returns”E
Throws
Section titled “Throws”NoSuchElementException if the deque is empty
Implementation of
Section titled “Implementation of”getLast()
Section titled “getLast()”getLast():
E
Defined in: src/queue/ArrayDeque.ts:396
Returns the last element. O(1).
Returns
Section titled “Returns”E
Throws
Section titled “Throws”NoSuchElementException if the deque is empty
Implementation of
Section titled “Implementation of”hashCode()
Section titled “hashCode()”hashCode():
number
Defined in: src/abstract/AbstractCollection.ts:190
Identity hash by default, consistent with the identity equals.
Returns
Section titled “Returns”number
Implementation of
Section titled “Implementation of”Inherited from
Section titled “Inherited from”isEmpty()
Section titled “isEmpty()”isEmpty():
boolean
Defined in: src/queue/ArrayDeque.ts:125
Returns true if this deque has no elements. O(1).
Returns
Section titled “Returns”boolean
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”iterator()
Section titled “iterator()”iterator():
Iterator<E>
Defined in: src/queue/ArrayDeque.ts:465
Returns a fail-fast iterator over the elements, head to tail. Its remove is O(n), shifting the shorter side.
Returns
Section titled “Returns”Iterator<E>
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”offer()
Section titled “offer()”offer(
e):boolean
Defined in: src/queue/ArrayDeque.ts:192
Inserts e at the end; same as addLast(e). Amortized O(1).
Parameters
Section titled “Parameters”E
Returns
Section titled “Returns”boolean
always true (the deque is unbounded)
Throws
Section titled “Throws”NullPointerException if e is null or undefined
Implementation of
Section titled “Implementation of”offerFirst()
Section titled “offerFirst()”offerFirst(
e):boolean
Defined in: src/queue/ArrayDeque.ts:162
Inserts e at the front; same as addFirst(e). Amortized O(1).
Parameters
Section titled “Parameters”E
Returns
Section titled “Returns”boolean
always true (the deque is unbounded)
Throws
Section titled “Throws”NullPointerException if e is null or undefined
Implementation of
Section titled “Implementation of”offerLast()
Section titled “offerLast()”offerLast(
e):boolean
Defined in: src/queue/ArrayDeque.ts:172
Inserts e at the end; same as addLast(e). Amortized O(1).
Parameters
Section titled “Parameters”E
Returns
Section titled “Returns”boolean
always true (the deque is unbounded)
Throws
Section titled “Throws”NullPointerException if e is null or undefined
Implementation of
Section titled “Implementation of”parallelStream()
Section titled “parallelStream()”parallelStream():
Stream<E>
Defined in: src/abstract/AbstractCollection.ts:180
Returns a stream over spliterator() flagged as parallel.
Returns
Section titled “Returns”Stream<E>
Implementation of
Section titled “Implementation of”Inherited from
Section titled “Inherited from”AbstractCollection.parallelStream
peek()
Section titled “peek()”peek():
E|null
Defined in: src/queue/ArrayDeque.ts:379
Returns the head without removing it, in O(1), or null if this deque is empty; same as peekFirst().
Returns
Section titled “Returns”E | null
Implementation of
Section titled “Implementation of”peekFirst()
Section titled “peekFirst()”peekFirst():
E|null
Defined in: src/queue/ArrayDeque.ts:368
Returns the first element without removing it, in O(1), or null if this deque is empty.
Returns
Section titled “Returns”E | null
Implementation of
Section titled “Implementation of”peekLast()
Section titled “peekLast()”peekLast():
E|null
Defined in: src/queue/ArrayDeque.ts:373
Returns the last element without removing it, in O(1), or null if this deque is empty.
Returns
Section titled “Returns”E | null
Implementation of
Section titled “Implementation of”poll()
Section titled “poll()”poll():
E|null
Defined in: src/queue/ArrayDeque.ts:267
Removes and returns the head in O(1), or null if this deque is empty; same as pollFirst().
Returns
Section titled “Returns”E | null
Implementation of
Section titled “Implementation of”pollFirst()
Section titled “pollFirst()”pollFirst():
E|null
Defined in: src/queue/ArrayDeque.ts:223
Removes and returns the first element in O(1), or null if this deque is empty.
Returns
Section titled “Returns”E | null
Implementation of
Section titled “Implementation of”pollLast()
Section titled “pollLast()”pollLast():
E|null
Defined in: src/queue/ArrayDeque.ts:235
Removes and returns the last element in O(1), or null if this deque is empty.
Returns
Section titled “Returns”E | null
Implementation of
Section titled “Implementation of”pop():
E
Defined in: src/queue/ArrayDeque.ts:275
Pops the top of the stack this deque represents, that is, removes and returns the first element; same as removeFirst(). O(1).
Returns
Section titled “Returns”E
Throws
Section titled “Throws”NoSuchElementException if the deque is empty
Implementation of
Section titled “Implementation of”push()
Section titled “push()”push(
e):void
Defined in: src/queue/ArrayDeque.ts:201
Pushes e onto the stack this deque represents, that is, inserts it at the front; same as addFirst(e). Amortized O(1).
Parameters
Section titled “Parameters”E
Returns
Section titled “Returns”void
Throws
Section titled “Throws”NullPointerException if e is null or undefined
Implementation of
Section titled “Implementation of”remove()
Section titled “remove()”Call Signature
Section titled “Call Signature”remove():
E
Defined in: src/queue/ArrayDeque.ts:283
Removes and returns the first element; same as removeFirst(). O(1).
Returns
Section titled “Returns”E
Throws
Section titled “Throws”NoSuchElementException if the deque is empty
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”Call Signature
Section titled “Call Signature”remove(
o):boolean
Defined in: src/queue/ArrayDeque.ts:289
Removes the first element equal to o (by Objects.equals), if present; same as
removeFirstOccurrence(o). O(n).
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”boolean
true if an element was removed
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”AbstractCollection.remove
removeAll()
Section titled “removeAll()”removeAll(
c):boolean
Defined in: src/abstract/AbstractCollection.ts:108
Removes every element that c contains, through the iterator’s remove(); returns true if any was removed.
Parameters
Section titled “Parameters”Collection<unknown>
Returns
Section titled “Returns”boolean
Throws
Section titled “Throws”NullPointerException if c is null
Implementation of
Section titled “Implementation of”Inherited from
Section titled “Inherited from”removeFirst()
Section titled “removeFirst()”removeFirst():
E
Defined in: src/queue/ArrayDeque.ts:250
Removes and returns the first element. O(1).
Returns
Section titled “Returns”E
Throws
Section titled “Throws”NoSuchElementException if the deque is empty
Implementation of
Section titled “Implementation of”removeFirstOccurrence()
Section titled “removeFirstOccurrence()”removeFirstOccurrence(
o):boolean
Defined in: src/queue/ArrayDeque.ts:300
Removes the first element (from head to tail) equal to o. O(n): a scan, then a shift of the shorter
side. null or undefined simply returns false.
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”boolean
true if an element was removed
Implementation of
Section titled “Implementation of”removeIf()
Section titled “removeIf()”removeIf(
filter):boolean
Defined in: src/queue/ArrayDeque.ts:326
Removes every element for which filter returns true, in O(n). Every element is tested before
anything is removed, so a throwing predicate, or one that modifies the deque, leaves the deque unchanged.
Parameters
Section titled “Parameters”filter
Section titled “filter”(e) => boolean
Returns
Section titled “Returns”boolean
true if any element was removed
Throws
Section titled “Throws”NullPointerException if filter is null
Throws
Section titled “Throws”ConcurrentModificationException if filter structurally modifies this deque
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”removeLast()
Section titled “removeLast()”removeLast():
E
Defined in: src/queue/ArrayDeque.ts:260
Removes and returns the last element. O(1).
Returns
Section titled “Returns”E
Throws
Section titled “Throws”NoSuchElementException if the deque is empty
Implementation of
Section titled “Implementation of”removeLastOccurrence()
Section titled “removeLastOccurrence()”removeLastOccurrence(
o):boolean
Defined in: src/queue/ArrayDeque.ts:312
Removes the last element (the first found from tail to head) equal to o. O(n): a scan, then a shift
of the shorter side. null or undefined simply returns false.
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”boolean
true if an element was removed
Implementation of
Section titled “Implementation of”retainAll()
Section titled “retainAll()”retainAll(
c):boolean
Defined in: src/abstract/AbstractCollection.ts:118
Removes every element that c does not contain, through the iterator’s remove(); returns true if any was
removed.
Parameters
Section titled “Parameters”Collection<unknown>
Returns
Section titled “Returns”boolean
Throws
Section titled “Throws”NullPointerException if c is null
Implementation of
Section titled “Implementation of”Inherited from
Section titled “Inherited from”reversed()
Section titled “reversed()”reversed():
Deque<E>
Defined in: src/queue/ArrayDeque.ts:492
Returns a live reverse-ordered view in O(1): its head is this deque’s tail, and changes through either side are visible in the other.
Returns
Section titled “Returns”Deque<E>
Implementation of
Section titled “Implementation of”size()
Section titled “size()”size():
number
Defined in: src/queue/ArrayDeque.ts:120
Returns the number of elements. O(1), computed from the head and tail indexes.
Returns
Section titled “Returns”number
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”spliterator()
Section titled “spliterator()”spliterator():
Spliterator<E>
Defined in: src/queue/ArrayDeque.ts:479
Returns a spliterator over the elements, used to build streams: ORDERED | SIZED | SUBSIZED | NONNULL,
head to tail, late-binding, splits in halves. It is not modCount-based: it traverses the range bound at
first use, and throws ConcurrentModificationException when it reaches a slot vacated in the meantime.
Returns
Section titled “Returns”Spliterator<E>
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”AbstractCollection.spliterator
stream()
Section titled “stream()”stream():
Stream<E>
Defined in: src/abstract/AbstractCollection.ts:175
Returns a sequential stream over spliterator().
Returns
Section titled “Returns”Stream<E>
Implementation of
Section titled “Implementation of”Inherited from
Section titled “Inherited from”toArray()
Section titled “toArray()”Call Signature
Section titled “Call Signature”toArray():
E[]
Defined in: src/queue/ArrayDeque.ts:418
Returns a new array with all elements, head to tail. Changing the array does not affect this deque. O(n).
Returns
Section titled “Returns”E[]
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”Call Signature
Section titled “Call Signature”toArray<
A>(generator):A
Defined in: src/queue/ArrayDeque.ts:423
Fills an array created by generator(size()) with the elements head to tail, and returns it. O(n).
Type Parameters
Section titled “Type Parameters”A extends WritableArrayLike<E>
Parameters
Section titled “Parameters”generator
Section titled “generator”(length) => A
creates the target array for a given length
Returns
Section titled “Returns”A
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”toString()
Section titled “toString()”toString():
string
Defined in: src/queue/ArrayDeque.ts:452
Returns the elements head to tail as [a, b, c]; an element that is this deque prints as (this Collection).
Returns
Section titled “Returns”string
Implementation of
Section titled “Implementation of”Overrides
Section titled “Overrides”
staticof<E>(…xs):ArrayDeque<E>
Defined in: src/queue/ArrayDeque.ts:102
Creates a deque holding xs, the first argument at the head.
Type Parameters
Section titled “Type Parameters”E
Parameters
Section titled “Parameters”…E[]
Returns
Section titled “Returns”ArrayDeque<E>
Throws
Section titled “Throws”NullPointerException if any argument is null or undefined