javascript-collections-framework
    Preparing search index...

    Class ArrayDeque<E>Sealed

    A double-ended queue stored in a circular array: adding at either end is amortised O(1) and removing is O(1), so it serves as a first-in-first-out queue (add + poll), as a stack (push + pop, at the end, as on an array) or as a buffer worked from both ends.

    null and undefined are not allowed as elements, so poll(), peek() and pop() returning undefined always means the deque is empty. Two deques are equal only when they are the same object; compare toArray() results to compare contents. Adding or removing while iterating throws ConcurrentModificationError; use a cursor to remove while iterating.

    The class is not designed to be subclassed.

    const queue = new ArrayDeque<string>()
    queue.add('a').add('b')
    queue.poll() // 'a'

    const stack = new ArrayDeque([1, 2])
    stack.push(3)
    stack.pop() // 3

    Type Parameters

    • E

    Implements

    Index
    • Creates a deque holding the elements of elements, in order (the first is the front); empty when elements is omitted, undefined or null (as new Set(null) is).

      Time: O(m) for m elements.

      Type Parameters

      • E

      Parameters

      • Optionalelements: Iterable<E, any, any> | null

        The initial elements.

      Returns ArrayDeque<E>

      TypeError when elements is neither iterable nor null/undefined.

      NullElementError when an element is null or undefined.

      IllegalStateError when the elements would not fit (over 2^30 in total).

      new ArrayDeque(['a', 'b']) // ArrayDeque(2) ['a', 'b']
      
    • get "[ARRAY_DEQUE_BRAND]"(): true

      Marks instances of this class so instanceof recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[COLLECTION_BRAND]"(): true

      Marks collections so isCollection recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[DEQUE_BRAND]"(): true

      Marks deques so isDeque recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[QUEUE_BRAND]"(): true

      Marks queues so isQueue recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[SEQUENCED_COLLECTION_BRAND]"(): true

      Marks sequenced collections so isSequencedCollection recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[toStringTag]"(): string

      'ArrayDeque', for Object.prototype.toString. Time: O(1).

      Returns string

    • get revision(): number

      A number that changes on every change to the contents and never on a read. Its exact values are unspecified. Time: O(1).

      Returns number

    • get size(): number

      The number of elements. Time: O(1).

      Returns number

    • The value-equality protocol; delegates to equals (identity). Time: O(1).

      Parameters

      • other: unknown

      Returns boolean

    • Node's inspect hook: ArrayDeque(2) [ 1, 2 ] in console.log. Time: as toString.

      Parameters

      • depth: number | null
      • options: object
      • inspect: (value: unknown, options: object) => string

      Returns string

    • Returns an iterator over the elements from first to last.

      Time: O(1) to create; O(1) per step.

      Returns IterableIterator<E>

      A new iterator.

      ConcurrentModificationError (from next()) when elements are added or removed after the iterator is created, including through a cursor.

      for (const element of new ArrayDeque([1, 2])) console.log(element)
      
    • Adds element at the end and returns the deque, so calls chain.

      Time: amortised O(1).

      Parameters

      • element: E

        The element to add.

      Returns this

      The deque.

      NullElementError when element is null or undefined.

      IllegalStateError when the deque already holds the most elements it can.

      new ArrayDeque<number>().add(1).add(2) // ArrayDeque(2) [1, 2]
      
    • Adds every element of elements at the end, in order, after reading and checking them all: if any is null or undefined, nothing is added.

      Time: amortised O(m) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to add.

      Returns boolean

      true when any element was added.

      TypeError when elements is not iterable.

      NullElementError when an element is null or undefined.

      IllegalStateError when the elements would not fit (over 2^30 in total).

      new ArrayDeque([1]).addAll([2, 3]) // true; [1, 2, 3]
      
    • Adds element at the front and returns the deque.

      Time: amortised O(1).

      Parameters

      • element: E

        The element to add.

      Returns this

      The deque.

      NullElementError when element is null or undefined.

      IllegalStateError when the deque already holds the most elements it can.

      new ArrayDeque([2]).addFirst(1) // ArrayDeque(2) [1, 2]
      
    • Adds element at the end and returns the deque; the same as add.

      Time: amortised O(1).

      Parameters

      • element: E

        The element to add.

      Returns this

      The deque.

      NullElementError when element is null or undefined.

      IllegalStateError when the deque already holds the most elements it can.

      new ArrayDeque([1]).addLast(2) // ArrayDeque(2) [1, 2]
      
    • Removes every element.

      Time: O(n).

      Returns void

      deque.clear() // deque.size === 0
      
    • Returns a new ArrayDeque with the same elements in the same order, independent of this one (the elements themselves are not copied).

      Time: O(n).

      Returns ArrayDeque<E>

      The copy.

      const copy = deque.copy()
      copy.poll() // deque is unchanged
    • Returns a cursor positioned before the first element; remove() removes the element next() returned. reversed().cursor() walks from the last element to the first.

      Time: O(1) to create; next O(1); remove O(n).

      Returns Cursor<E>

      A new cursor.

      const cursor = deque.cursor()
      while (cursor.hasNext()) if (cursor.next() < 0) cursor.remove()
    • Removes the first element equal to element under SameValueZero, searching from the front. reversed().delete(element) removes the last one instead.

      Time: O(n).

      Parameters

      • element: E

        The value to remove.

      Returns boolean

      true when an element was removed.

      new ArrayDeque([1, 2, 1]).delete(1) // true; [2, 1]
      
    • Returns whether other is this deque. Deques compare by identity; compare toArray() to compare contents. Called through a Proxy of the deque, it is true for that proxy too; the deque itself does not know its proxies, so deque.equals(proxy) is false.

      Time: O(1).

      Parameters

      • other: unknown

        Any value.

      Returns boolean

      true when other is this deque.

      new ArrayDeque([1]).equals(new ArrayDeque([1])) // false
      
    • Calls action(element) for each element from first to last, with this set to thisArg.

      Time: O(n) plus the calls.

      Parameters

      • action: (element: E) => void

        Called with each element.

      • OptionalthisArg: unknown

        The this value for action.

      Returns void

      TypeError when action is not a function.

      ConcurrentModificationError when action adds or removes elements.

      new ArrayDeque(['a', 'b']).forEach((s) => console.log(s))
      
    • Returns the first element.

      Time: O(1).

      Returns E

      The first element.

      NoSuchElementError when the deque is empty.

      new ArrayDeque([1, 2]).getFirst() // 1
      
    • Returns the last element.

      Time: O(1).

      Returns E

      The last element.

      NoSuchElementError when the deque is empty.

      new ArrayDeque([1, 2]).getLast() // 2
      
    • Returns whether some element is equal to element under SameValueZero; always false for null and undefined.

      Time: O(n).

      Parameters

      • element: E

        The value to look for.

      Returns boolean

      true when present.

      new ArrayDeque([1, 2]).has(2) // true
      
    • Returns whether the deque has no elements.

      Time: O(1).

      Returns boolean

      size === 0.

      new ArrayDeque().isEmpty() // true
      
    • Returns the first element without removing it, or undefined when the deque is empty.

      Time: O(1).

      Returns E | undefined

      The first element, or undefined.

      new ArrayDeque([1, 2]).peek() // 1
      
    • Returns the last element without removing it, or undefined when the deque is empty.

      Time: O(1).

      Returns E | undefined

      The last element, or undefined.

      new ArrayDeque([1, 2]).peekLast() // 2
      
    • Removes and returns the first element, or returns undefined when the deque is empty.

      Time: O(1).

      Returns E | undefined

      The removed element, or undefined.

      new ArrayDeque(['a', 'b']).poll() // 'a'
      
    • Removes and returns the last element, or returns undefined when the deque is empty, as Array.prototype.pop does.

      Time: O(1).

      Returns E | undefined

      The removed element, or undefined.

      new ArrayDeque([1, 2]).pop() // 2
      
    • Adds element at the end, as add does, and returns the deque. Unlike Array.prototype.push, it returns the deque, not the new length.

      Time: amortised O(1).

      Parameters

      • element: E

        The element to add.

      Returns this

      The deque.

      NullElementError when element is null or undefined.

      IllegalStateError when the deque already holds the most elements it can.

      new ArrayDeque([1]).push(2) // ArrayDeque(2) [1, 2]
      
    • Removes every element equal to one in elements (SameValueZero). Decides first, so an error while deciding removes nothing.

      Time: O(n + m) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to remove.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable.

      new ArrayDeque([1, 2, 1]).removeAll([1]) // true; [2]
      
    • Removes and returns the first element.

      Time: O(1).

      Returns E

      The removed element.

      NoSuchElementError when the deque is empty.

      new ArrayDeque([1, 2]).removeFirst() // 1
      
    • Calls predicate(element) on every element, then removes those for which it returned a truthy value; an error from predicate removes nothing.

      Time: O(n) plus the calls.

      Parameters

      • predicate: (element: E) => unknown

        Called with each element.

      Returns boolean

      true when any element was removed.

      TypeError when predicate is not a function.

      ConcurrentModificationError when predicate adds or removes elements.

      new ArrayDeque([1, -2, 3]).removeIf((n) => n < 0) // true; [1, 3]
      
    • Removes and returns the last element.

      Time: O(1).

      Returns E

      The removed element.

      NoSuchElementError when the deque is empty.

      new ArrayDeque([1, 2]).removeLast() // 2
      
    • Removes every element not equal to one in elements (SameValueZero). Decides first, so an error while deciding removes nothing.

      Time: O(n + m) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to keep.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable.

      new ArrayDeque([1, 2, 3]).retainAll([2]) // true; [2]
      
    • Returns a live view of this deque in reverse order: its first element is this deque's last, and changes through either show in both (the view's push adds at this deque's front, its poll is this deque's pop). reversed().reversed() returns this deque.

      Time: O(1); each operation on the view costs what its mirror costs.

      Returns Deque<E>

      The reversed view.

      const deque = new ArrayDeque([1, 2, 3])
      deque.reversed().peek() // 3
      deque.reversed().delete(1) // removes the last 1
    • Returns a new array of the elements, from first to last.

      Time: O(n).

      Returns E[]

      A new array.

      new ArrayDeque([1, 2]).toArray() // [1, 2]
      
    • Returns a new array of the elements, so JSON.stringify writes the deque as an array.

      Time: O(n).

      Returns E[]

      A new array.

      JSON.stringify(new ArrayDeque([1, 2])) // '[1,2]'
      
    • Returns the class name, size and elements, e.g. ArrayDeque(2) [1, 'a'].

      Time: O(length of the output) for flat contents.

      Returns string

      The text.

      String(new ArrayDeque([1, 'a'])) // "ArrayDeque(2) [1, 'a']"
      
    • Returns whether value is an ArrayDeque, including one created by another copy of the library with the same major version. A reversed view is not an ArrayDeque.

      Time: O(1).

      Parameters

      • value: unknown

        The value on the left of instanceof.

      Returns boolean

      true when value is an ArrayDeque.

      new ArrayDeque() instanceof ArrayDeque // true
      
    • Returns a new deque of the elements of elements, in order, like Array.from but for iterables only.

      Time: O(m) for m elements.

      Type Parameters

      • T

      Parameters

      • elements: Iterable<T>

        The elements.

      Returns ArrayDeque<T>

      A new deque.

      TypeError when elements is not iterable (null, undefined and array-likes included).

      NullElementError when an element is null or undefined.

      IllegalStateError when the elements would not fit (over 2^30 in total).

      ArrayDeque.from(new Set([1, 2])) // ArrayDeque(2) [1, 2]
      
    • Returns a new deque of mapFn(element, index) for each element of elements, in order, with this set to thisArg, as Array.from does. elements is read while mapFn runs, so items mapFn appends to an array source are included.

      Time: O(m) plus the calls.

      Type Parameters

      • T
      • U

      Parameters

      • elements: Iterable<T>

        The elements.

      • mapFn: (element: T, index: number) => U

        Called with each element and its index; its result is stored.

      • OptionalthisArg: unknown

        The this value for mapFn.

      Returns ArrayDeque<U>

      A new deque.

      TypeError when mapFn is not a function (checked first) or elements is not iterable.

      NullElementError when mapFn returns null or undefined.

      IllegalStateError when the elements would not fit (over 2^30 in total).

      Error whatever mapFn throws; no collection is returned.

      ArrayDeque.from([1, 2], (n) => n * 10) // ArrayDeque(2) [10, 20]