javascript-collections-framework
    Preparing search index...

    Interface MapCollection<K, V>

    A map: the native Map methods plus read-modify-write helpers and live views.

    Callbacks take the value first and the map last, as forEach does. In compute, computeIfPresent and merge, a result of undefined removes the mapping.

    function count<K>(counts: MapCollection<K, number>, key: K): void {
    counts.merge(key, 1, (old, one) => old + one)
    }
    interface MapCollection<K, V> {
        revision: number;
        size: number;
        "[equalsSymbol]"(other: unknown): boolean;
        "[hashCodeSymbol]"(hash: HashFunction): number;
        "[iterator]"(): Iterator<[K, V], any, any>;
        clear(): void;
        compute(
            key: K,
            fn: (
                value: V | undefined,
                key: K,
                map: MapCollection<K, V>,
            ) => V | undefined,
        ): V | undefined;
        computeIfPresent(
            key: K,
            fn: (value: V, key: K, map: MapCollection<K, V>) => V | undefined,
        ): V | undefined;
        containsValue(value: V): boolean;
        delete(key: K): boolean;
        entries(): IterableIterator<[K, V]>;
        entrySet(): SetCollection<MapEntry<K, V>>;
        equals(other: unknown): boolean;
        forEach(
            action: (value: V, key: K, map: MapCollection<K, V>) => void,
            thisArg?: unknown,
        ): void;
        get(key: K): V | undefined;
        getOrDefault<D>(key: K, fallback: D): V | D;
        getOrInsert(key: K, value: V): V;
        getOrInsertComputed(key: K, fn: (key: K) => V): V;
        has(key: K): boolean;
        isEmpty(): boolean;
        keys(): IterableIterator<K>;
        keySet(): SetCollection<K>;
        merge(
            key: K,
            value: V,
            fn: (old: V, value: V, key: K, map: MapCollection<K, V>) => V | undefined,
        ): V | undefined;
        put(key: K, value: V): V | undefined;
        putAll(entries: Iterable<MapEntry<K, V> | readonly [K, V]>): void;
        remove(key: K): V | undefined;
        replace(key: K, value: V): V | undefined;
        replaceAll(fn: (value: V, key: K, map: MapCollection<K, V>) => V): void;
        set(key: K, value: V): this;
        toJSON(): [K, V][];
        toString(): string;
        valueCollection(): Collection<V>;
        values(): IterableIterator<V>;
    }

    Type Parameters

    • K
    • V

    Hierarchy (View Summary)

    Implemented by

    Index
    revision: number

    A number that changes on every change to the mappings — adding or removing one, or writing a value — and never on a read, except that a read moving a mapping of an access-ordered map changes it too. Its exact values are unspecified. Time: O(1).

    size: number

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

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

      Parameters

      • other: unknown

      Returns boolean

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

      Returns void

    • Maps key to fn(currentValueOrUndefined, key, map), or removes the mapping when the result is undefined, and returns the result.

      Time: as set, plus the call.

      Parameters

      • key: K

        The key.

      • fn: (value: V | undefined, key: K, map: MapCollection<K, V>) => V | undefined

        Computes the new value.

      Returns V | undefined

      The result of fn.

      TypeError when fn is not a function.

      ConcurrentModificationError when fn adds or removes mappings.

    • When key is present, maps it to fn(value, key, map), or removes the mapping when the result is undefined, and returns the result; otherwise returns undefined without calling fn.

      Time: as set, plus the call.

      Parameters

      • key: K

        The key.

      • fn: (value: V, key: K, map: MapCollection<K, V>) => V | undefined

        Computes the new value.

      Returns V | undefined

      The result of fn, or undefined.

      TypeError when fn is not a function.

      ConcurrentModificationError when fn adds or removes mappings.

    • Returns whether some value is SameValueZero-equal to value.

      Time: O(n).

      Parameters

      • value: V

        The value to look for.

      Returns boolean

      true when a mapping has that value.

    • Removes the mapping of key and returns whether there was one.

      Time: as set.

      Parameters

      • key: K

        The key.

      Returns boolean

      true when a mapping was removed.

    • Returns an iterator over new [key, value] arrays.

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

      Returns IterableIterator<[K, V]>

      A new iterator.

    • Returns whether other is a map with the same size that maps every key of this map to an equal value, and whose keys this map all maps to equal values.

      Time: as the map documents — O(n) average for a HashMap, O(n log n) for a TreeMap — plus the protocol methods.

      Parameters

      • other: unknown

        Any value.

      Returns boolean

      true when other is an equal map.

    • Calls action(value, key, map) for each mapping, with this set to thisArg, as native Map does.

      Time: O(n) plus the calls.

      Parameters

      • action: (value: V, key: K, map: MapCollection<K, V>) => void

        Called with each value, its key and the map.

      • OptionalthisArg: unknown

        The this value for action.

      Returns void

      TypeError when action is not a function.

    • Returns the value mapped to a key equal to key, or undefined when there is none.

      Time: as the implementation documents.

      Parameters

      • key: K

        The key to look up.

      Returns V | undefined

      The value, or undefined.

    • Returns the value mapped to key when the key is present (even when that value is undefined), and fallback otherwise.

      Time: as get.

      Type Parameters

      • D

      Parameters

      • key: K

        The key to look up.

      • fallback: D

        The value to return when the key is absent.

      Returns V | D

      The mapped value, or fallback.

    • Returns the value of key when present; otherwise maps key to value and returns value, as native Map.prototype.getOrInsert does.

      Time: as set.

      Parameters

      • key: K

        The key.

      • value: V

        The value to insert when the key is absent.

      Returns V

      The mapped value.

    • Returns the value of key when present; otherwise calls fn(key), maps key to its result and returns it, as native Map.prototype.getOrInsertComputed does.

      Time: as set, plus the call.

      Parameters

      • key: K

        The key.

      • fn: (key: K) => V

        Computes the value to insert.

      Returns V

      The mapped value.

      TypeError when fn is not a function, even when the key is present.

    • Returns whether a key equal to key is present.

      Time: as get.

      Parameters

      • key: K

        The key to look for.

      Returns boolean

      true when the key is present.

    • Returns whether the map has no mappings.

      Time: O(1).

      Returns boolean

      size === 0.

    • Returns an iterator over the keys.

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

      Returns IterableIterator<K>

      A new iterator.

    • Maps key to value when it is absent; otherwise to fn(old, value, key, map), removing the mapping when that result is undefined. Returns the stored value, or undefined.

      Time: as set, plus the call.

      Parameters

      • key: K

        The key.

      • value: V

        The value to store, or to combine with the present one.

      • fn: (old: V, value: V, key: K, map: MapCollection<K, V>) => V | undefined

        Combines the present value with value.

      Returns V | undefined

      The stored value, or undefined when the mapping was removed.

      TypeError when fn is not a function.

      ConcurrentModificationError when fn adds or removes mappings.

    • Maps key to value and returns the previous value, or undefined when the key was absent.

      Time: as set.

      Parameters

      • key: K

        The key.

      • value: V

        The value.

      Returns V | undefined

      The previous value, or undefined.

    • Sets every mapping of entries in order, after reading and checking them all.

      Time: as the map documents — O(m) average for a HashMap, O(m log(n + m)) for a TreeMap — for m mappings.

      Parameters

      • entries: Iterable<MapEntry<K, V> | readonly [K, V]>

        [key, value] pairs, map entries, a native Map or a map.

      Returns void

      TypeError when entries is not iterable or an item is not a pair or an entry.

    • Removes the mapping of key and returns its value, or undefined when there was none.

      Time: as set.

      Parameters

      • key: K

        The key.

      Returns V | undefined

      The removed value, or undefined.

    • Replaces the value of key only when the key is present, and returns the previous value, or undefined.

      Time: as set.

      Parameters

      • key: K

        The key.

      • value: V

        The new value.

      Returns V | undefined

      The previous value, or undefined.

    • Replaces each value with fn(value, key, map), in iteration order.

      Time: O(n) plus the calls.

      Parameters

      Returns void

      TypeError when fn is not a function.

      ConcurrentModificationError when fn adds or removes mappings.

    • Maps key to value and returns the map, so calls chain.

      Time: as the implementation documents.

      Parameters

      • key: K

        The key.

      • value: V

        The value.

      Returns this

      The map.

    • Returns a new array of [key, value] arrays, so JSON.stringify writes the map as pairs.

      Time: O(n).

      Returns [K, V][]

      A new array.

    • Returns the class name, size and mappings, e.g. HashMap(1) {'a' => 1}.

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

      Returns string

      The text.

    • Returns an iterator over the values.

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

      Returns IterableIterator<V>

      A new iterator.