Skip to content
Buy me a coffee

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.

E

new ArrayDeque<E>(): ArrayDeque<E>

Defined in: src/queue/ArrayDeque.ts:72

Creates an empty deque with the default 16-slot buffer.

ArrayDeque<E>

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

number

expected number of elements

ArrayDeque<E>

AbstractCollection<E>.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).

Collection<E>

ArrayDeque<E>

NullPointerException if c is null or undefined, or holds null or undefined

AbstractCollection<E>.constructor

[INSPECT](): string

Defined in: src/abstract/AbstractCollection.ts:195

Node’s util.inspect hook: console.log prints the toString() form.

string

AbstractCollection.[INSPECT]


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

IterableIterator<E>

Deque.[iterator]

AbstractCollection.[iterator]


add(e): boolean

Defined in: src/queue/ArrayDeque.ts:182

Inserts e at the end; same as addLast(e). Amortized O(1).

E

boolean

always true

NullPointerException if e is null or undefined

Deque.add

AbstractCollection.add


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.

Collection<E>

boolean

true if this deque changed (c was not empty)

NullPointerException if c is null, or holds null or undefined

Deque.addAll

AbstractCollection.addAll


addFirst(e): void

Defined in: src/queue/ArrayDeque.ts:135

Inserts e at the front, in amortized O(1).

E

void

NullPointerException if e is null or undefined

Deque.addFirst


addLast(e): void

Defined in: src/queue/ArrayDeque.ts:148

Inserts e at the end, in amortized O(1).

E

void

NullPointerException if e is null or undefined

Deque.addLast


clear(): void

Defined in: src/queue/ArrayDeque.ts:355

Removes all elements. O(n); keeps the current capacity, since the buffer never shrinks.

void

Deque.clear

AbstractCollection.clear


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

ArrayDeque<E>


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.

unknown

boolean

Deque.contains

AbstractCollection.contains


containsAll(c): boolean

Defined in: src/abstract/AbstractCollection.ts:83

Returns true if this collection contains every element of c (calls contains for each).

Collection<unknown>

boolean

NullPointerException if c is null

Deque.containsAll

AbstractCollection.containsAll


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.

Iterator<E>

Deque.descendingIterator


element(): E

Defined in: src/queue/ArrayDeque.ts:406

Returns the head without removing it, in O(1); same as getFirst().

E

NoSuchElementException if the deque is empty

Deque.element


equals(o): boolean

Defined in: src/abstract/AbstractCollection.ts:185

Identity by default; List/Set/Map subclasses override with value equality.

unknown

boolean

Deque.equals

AbstractCollection.equals


forEach(action): void

Defined in: src/queue/ArrayDeque.ts:440

Calls action for each element, head to tail.

(e) => void

void

NullPointerException if action is null

ConcurrentModificationException if action structurally modifies this deque

Deque.forEach

AbstractCollection.forEach


getFirst(): E

Defined in: src/queue/ArrayDeque.ts:387

Returns the first element. O(1).

E

NoSuchElementException if the deque is empty

Deque.getFirst


getLast(): E

Defined in: src/queue/ArrayDeque.ts:396

Returns the last element. O(1).

E

NoSuchElementException if the deque is empty

Deque.getLast


hashCode(): number

Defined in: src/abstract/AbstractCollection.ts:190

Identity hash by default, consistent with the identity equals.

number

Deque.hashCode

AbstractCollection.hashCode


isEmpty(): boolean

Defined in: src/queue/ArrayDeque.ts:125

Returns true if this deque has no elements. O(1).

boolean

Deque.isEmpty

AbstractCollection.isEmpty


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.

Iterator<E>

Deque.iterator

AbstractCollection.iterator


offer(e): boolean

Defined in: src/queue/ArrayDeque.ts:192

Inserts e at the end; same as addLast(e). Amortized O(1).

E

boolean

always true (the deque is unbounded)

NullPointerException if e is null or undefined

Deque.offer


offerFirst(e): boolean

Defined in: src/queue/ArrayDeque.ts:162

Inserts e at the front; same as addFirst(e). Amortized O(1).

E

boolean

always true (the deque is unbounded)

NullPointerException if e is null or undefined

Deque.offerFirst


offerLast(e): boolean

Defined in: src/queue/ArrayDeque.ts:172

Inserts e at the end; same as addLast(e). Amortized O(1).

E

boolean

always true (the deque is unbounded)

NullPointerException if e is null or undefined

Deque.offerLast


parallelStream(): Stream<E>

Defined in: src/abstract/AbstractCollection.ts:180

Returns a stream over spliterator() flagged as parallel.

Stream<E>

Deque.parallelStream

AbstractCollection.parallelStream


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

E | null

Deque.peek


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.

E | null

Deque.peekFirst


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.

E | null

Deque.peekLast


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

E | null

Deque.poll


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.

E | null

Deque.pollFirst


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.

E | null

Deque.pollLast


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

E

NoSuchElementException if the deque is empty

Deque.pop


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

E

void

NullPointerException if e is null or undefined

Deque.push


remove(): E

Defined in: src/queue/ArrayDeque.ts:283

Removes and returns the first element; same as removeFirst(). O(1).

E

NoSuchElementException if the deque is empty

Deque.remove

AbstractCollection.remove

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

unknown

boolean

true if an element was removed

Deque.remove

AbstractCollection.remove


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.

Collection<unknown>

boolean

NullPointerException if c is null

Deque.removeAll

AbstractCollection.removeAll


removeFirst(): E

Defined in: src/queue/ArrayDeque.ts:250

Removes and returns the first element. O(1).

E

NoSuchElementException if the deque is empty

Deque.removeFirst


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.

unknown

boolean

true if an element was removed

Deque.removeFirstOccurrence


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.

(e) => boolean

boolean

true if any element was removed

NullPointerException if filter is null

ConcurrentModificationException if filter structurally modifies this deque

Deque.removeIf

AbstractCollection.removeIf


removeLast(): E

Defined in: src/queue/ArrayDeque.ts:260

Removes and returns the last element. O(1).

E

NoSuchElementException if the deque is empty

Deque.removeLast


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.

unknown

boolean

true if an element was removed

Deque.removeLastOccurrence


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.

Collection<unknown>

boolean

NullPointerException if c is null

Deque.retainAll

AbstractCollection.retainAll


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.

Deque<E>

Deque.reversed


size(): number

Defined in: src/queue/ArrayDeque.ts:120

Returns the number of elements. O(1), computed from the head and tail indexes.

number

Deque.size

AbstractCollection.size


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.

Spliterator<E>

Deque.spliterator

AbstractCollection.spliterator


stream(): Stream<E>

Defined in: src/abstract/AbstractCollection.ts:175

Returns a sequential stream over spliterator().

Stream<E>

Deque.stream

AbstractCollection.stream


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

E[]

Deque.toArray

AbstractCollection.toArray

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

A extends WritableArrayLike<E>

(length) => A

creates the target array for a given length

A

Deque.toArray

AbstractCollection.toArray


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

string

Deque.toString

AbstractCollection.toString


static of<E>(…xs): ArrayDeque<E>

Defined in: src/queue/ArrayDeque.ts:102

Creates a deque holding xs, the first argument at the head.

E

…E[]

ArrayDeque<E>

NullPointerException if any argument is null or undefined