javascript-collections-framework
    Preparing search index...

    Interface List<E>

    A list that can be changed: ordered, indexed from 0, duplicates allowed.

    add appends; insert puts an element at a position. delete(element) removes by value; removeAt(index) by position.

    function moveToFront<E>(list: List<E>, element: E): void {
    if (list.delete(element)) list.addFirst(element)
    }
    interface List<E> {
        revision: number;
        size: number;
        "[equalsSymbol]"(other: unknown): boolean;
        "[hashCodeSymbol]"(hash: HashFunction): number;
        "[iterator]"(): IterableIterator<E>;
        add(element: E): this;
        addAll(elements: Iterable<E>): boolean;
        addFirst(element: E): this;
        addLast(element: E): this;
        clear(): void;
        cursor(): Cursor<E>;
        delete(element: E): boolean;
        equals(other: unknown): boolean;
        forEach(
            action: (element: E, index: number, list: List<E>) => void,
            thisArg?: unknown,
        ): void;
        get(index: number): E;
        getFirst(): E;
        getLast(): E;
        has(element: E): boolean;
        indexOf(element: E): number;
        insert(index: number, element: E): this;
        insertAll(index: number, elements: Iterable<E>): boolean;
        isEmpty(): boolean;
        lastIndexOf(element: E): number;
        listCursor(index?: number): ListCursor<E>;
        removeAll(elements: Iterable<E>): boolean;
        removeAt(index: number): E;
        removeFirst(): E;
        removeIf(
            predicate: (element: E, index: number, list: List<E>) => unknown,
        ): boolean;
        removeLast(): E;
        replaceAll(op: (element: E, index: number, list: List<E>) => E): void;
        retainAll(elements: Iterable<E>): boolean;
        reversed(): List<E>;
        set(index: number, element: E): E;
        sort(comparator?: Comparator<E>): this;
        subList(from: number, to: number): List<E>;
        toArray(): E[];
        toJSON(): E[];
        toString(): string;
    }

    Type Parameters

    • E

    Hierarchy (View Summary)

    Implemented by

    Index
    revision: number

    A number that changes on every change to the contents — adding, removing or replacing an element — and never on a read. Watch it to detect changes; its exact values are unspecified. Time: O(1).

    size: number

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

    • The value-equality protocol; delegates to equals. Time: as equals.

      Parameters

      • other: unknown

      Returns boolean

    • Returns an iterator over the elements in iteration order.

      Time: O(1) to create; each step amortised O(1) unless the implementation documents otherwise.

      Returns IterableIterator<E>

      A new iterator.

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

    • Adds element and returns the collection, so calls chain. A collection that refuses duplicates is left unchanged. Time: as the implementation documents.

      Parameters

      • element: E

        The element to add.

      Returns this

      The collection.

    • Adds every element of elements, in order, after reading them all. If an error is thrown part-way, the elements added before it stay.

      Time: O(m) plus m additions.

      Parameters

      • elements: Iterable<E>

        The elements to add.

      Returns boolean

      true when any element was added.

      TypeError when elements is not iterable.

    • Adds element at the front and returns the collection.

      Time: as the implementation documents.

      Parameters

      • element: E

        The element to add.

      Returns this

      The collection.

    • Adds element at the end and returns the collection.

      Time: as the implementation documents.

      Parameters

      • element: E

        The element to add.

      Returns this

      The collection.

    • Removes every element. Time: as the implementation documents.

      Returns void

    • Removes one element equal to element (the first, where order is defined). Time: as the implementation documents.

      Parameters

      • element: E

        The value to remove.

      Returns boolean

      true when an element was removed.

    • Returns whether other is equal to this collection, as the collection's family defines; otherwise only the collection itself. Time: as the implementation documents.

      Parameters

      • other: unknown

        Any value.

      Returns boolean

      true when equal.

    • Calls action(element, index, list) for each element in index order, with this set to thisArg, as Array.prototype.forEach does.

      Time: O(n) plus the calls.

      Parameters

      • action: (element: E, index: number, list: List<E>) => void

        Called with each element, its index and the list.

      • OptionalthisArg: unknown

        The this value for action.

      Returns void

      TypeError when action is not a function.

      ConcurrentModificationError when action adds or removes elements.

    • Returns the element at index.

      Time: as the implementation documents.

      Parameters

      • index: number

        An integer with 0 <= index < size.

      Returns E

      The element.

      IndexOutOfBoundsError when index is not an integer in range.

    • Returns the first element.

      Time: as the implementation documents.

      Returns E

      The first element.

      NoSuchElementError when the collection is empty.

    • Returns the last element.

      Time: as the implementation documents.

      Returns E

      The last element.

      NoSuchElementError when the collection is empty.

    • Returns whether some element is equal to element under the collection's equality (SameValueZero unless the collection says otherwise). Time: as the implementation documents.

      Parameters

      • element: E

        The value to look for.

      Returns boolean

      true when an equal element is present.

    • Returns the lowest index whose element is equal to element under SameValueZero (so NaN is found), or -1.

      Time: O(n).

      Parameters

      • element: E

        The value to look for.

      Returns number

      The index, or -1.

    • Inserts element at index, shifting later elements up by one, and returns the list.

      Time: as the implementation documents.

      Parameters

      • index: number

        An integer with 0 <= index <= size.

      • element: E

        The element to insert.

      Returns this

      The list.

      IndexOutOfBoundsError when index is not an integer in range.

    • Inserts every element of elements at index, in order, after reading them all.

      Time: as the implementation documents.

      Parameters

      • index: number

        An integer with 0 <= index <= size.

      • elements: Iterable<E>

        The elements to insert.

      Returns boolean

      true when any element was inserted.

      IndexOutOfBoundsError when index is not an integer in range (checked first).

      TypeError when elements is not iterable.

      ConcurrentModificationError when reading elements changed the list.

    • Returns whether the collection has no elements.

      Time: O(1).

      Returns boolean

      size === 0.

    • Returns the highest index whose element is equal to element under SameValueZero, or -1.

      Time: O(n).

      Parameters

      • element: E

        The value to look for.

      Returns number

      The index, or -1.

    • Returns a cursor positioned before index (0 when omitted).

      Time: O(1).

      Parameters

      • Optionalindex: number

        An integer with 0 <= index <= size.

      Returns ListCursor<E>

      A new cursor.

      IndexOutOfBoundsError when index is not an integer in range.

    • Removes every element equal to one in elements. Decides first, so an error while deciding removes nothing. Time: as the implementation documents.

      Parameters

      • elements: Iterable<E>

        The elements to remove.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable.

      ConcurrentModificationError when your code adds or removes elements while it decides.

    • Removes and returns the element at index, shifting later elements down by one.

      Time: as the implementation documents.

      Parameters

      • index: number

        An integer with 0 <= index < size.

      Returns E

      The removed element.

      IndexOutOfBoundsError when index is not an integer in range.

    • Removes and returns the first element.

      Time: as the implementation documents.

      Returns E

      The removed element.

      NoSuchElementError when the collection is empty.

    • Calls predicate(element, index, list) on every element (indexes before any removal), 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, index: number, list: List<E>) => unknown

        Called with each element, its index and the list.

      Returns boolean

      true when any element was removed.

      TypeError when predicate is not a function.

      ConcurrentModificationError when predicate adds or removes elements.

    • Removes and returns the last element.

      Time: as the implementation documents.

      Returns E

      The removed element.

      NoSuchElementError when the collection is empty.

    • Replaces each element with op(element, index, list), in index order. If op throws, the elements before it are already replaced and the rest are unchanged. Open iterators stay valid.

      Time: O(n) plus the calls.

      Parameters

      • op: (element: E, index: number, list: List<E>) => E

        Returns the replacement for each element.

      Returns void

      TypeError when op is not a function.

      ConcurrentModificationError when op adds or removes elements.

    • Removes every element not equal to one in elements. Decides first, so an error while deciding removes nothing. Time: as the implementation documents.

      Parameters

      • elements: Iterable<E>

        The elements to keep.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable.

      ConcurrentModificationError when your code adds or removes elements while it decides.

    • Returns a live view of this list in reverse order; changes through the view reach this list at the mirrored position.

      Time: O(1).

      Returns List<E>

      The reversed view.

    • Replaces the element at index and returns the old one. revision changes; open iterators stay valid.

      Time: as the implementation documents.

      Parameters

      • index: number

        An integer with 0 <= index < size.

      • element: E

        The new element.

      Returns E

      The element that was at index.

      IndexOutOfBoundsError when index is not an integer in range.

    • Sorts the list in place, keeping equal elements in their order (stable), and returns it. Without a comparator, natural order is used. null and undefined reach the comparator like any other element. If the comparator throws or returns something other than a number, the list is left as it was.

      Time: O(n log n); at most n·⌈log₂ n⌉ comparator calls.

      Parameters

      • Optionalcomparator: Comparator<E>

        The order; natural order when omitted.

      Returns this

      The list.

      TypeError when comparator is neither a function nor undefined, or returns NaN or a non-number.

      ClassCastError when natural order cannot compare two elements.

      NullElementError when natural order meets null or undefined.

      ConcurrentModificationError when the comparator adds or removes elements.

    • Returns a live view of the elements from from (inclusive) to to (exclusive); changes through the view reach this list. Once this list's structure changes other than through the view, the view throws ConcurrentModificationError.

      Time: O(1).

      Parameters

      • from: number

        Start of the range, inclusive.

      • to: number

        End of the range, exclusive.

      Returns List<E>

      The view.

      IndexOutOfBoundsError when from < 0, to > size or a bound is not an integer.

      IllegalArgumentError when from > to.

    • Returns a new array of the elements in iteration order; changing it does not change the collection.

      Time: O(n).

      Returns E[]

      A new array.

    • Returns a new array of the elements, so JSON.stringify writes the collection as an array.

      Time: O(n).

      Returns E[]

      A new array.

    • Returns the class name, size and elements, e.g. ArrayList(3) [1, 2, 'a'].

      Time: O(length of the output) for flat contents; each level of nested arrays or collections copies its text once more.

      Returns string

      The text.