Class HashMap<K, V, R>

Hash-based map. Supports object keys and custom hashing; offers O(1) average set/get/has.

Time O(1), Space O(1)

// should maintain insertion order
const linkedHashMap = new LinkedHashMap<number, string>();
linkedHashMap.set(1, 'A');
linkedHashMap.set(2, 'B');
linkedHashMap.set(3, 'C');

const result = Array.from(linkedHashMap);
console.log(result); // [
// [1, 'A'],
// [2, 'B'],
// [3, 'C']
// ]
// fast lookup of values by key
const hashMap = new HashMap<number, string>();
hashMap.set(1, 'A');
hashMap.set(2, 'B');
hashMap.set(3, 'C');

console.log(hashMap.get(1)); // 'A'
console.log(hashMap.get(2)); // 'B'
console.log(hashMap.get(3)); // 'C'
console.log(hashMap.get(99)); // undefined
// remove duplicates when adding multiple entries
const hashMap = new HashMap<number, string>();
hashMap.set(1, 'A');
hashMap.set(2, 'B');
hashMap.set(1, 'C'); // Update value for key 1

console.log(hashMap.size); // 2
console.log(hashMap.get(1)); // 'C'
console.log(hashMap.get(2)); // 'B'
// count occurrences of keys
const data = [1, 2, 1, 3, 2, 1];

const countMap = new HashMap<number, number>();
for (const key of data) {
countMap.set(key, (countMap.get(key) || 0) + 1);
}

console.log(countMap.get(1)); // 3
console.log(countMap.get(2)); // 2
console.log(countMap.get(3)); // 1

Type Parameters

  • K = any
  • V = any
  • R = [K, V]
    1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key map to a value.
    2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
    3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the new one will replace the old entry.
    4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.

Hierarchy (view full)

Constructors

  • Create a HashMap and optionally bulk-insert entries.

    Type Parameters

    • K = any
    • V = any
    • R = [K, V]

    Parameters

    • OptionalentryOrRawElements: Iterable<[K, V] | R, any, any> = []

      Iterable of entries or raw elements to insert.

    • Optionaloptions: HashMapOptions<K, V, R>

      Options: hash function and optional record-to-entry converter.

    Returns HashMap<K, V, R>

    New HashMap instance.

    Time O(N), Space O(N)

Accessors

  • get hashFn(): ((key: K) => string)
  • Get the current hash function for non-object keys.

    Returns ((key: K) => string)

    Hash function.

      • (key): string
      • Parameters

        • key: K

        Returns string

    Time O(1), Space O(1)

  • get objMap(): Map<object, V>
  • Get the internal Map used for object/function keys.

    Returns Map<object, V>

    Map of object→value.

    Time O(1), Space O(1)

  • get size(): number
  • Get the number of distinct keys stored.

    Returns number

    Current size.

    Time O(1), Space O(1)

  • get store(): {
        [p: string]: HashMapStoreItem<K, V>;
    }
  • Get the internal store for non-object keys.

    Returns {
        [p: string]: HashMapStoreItem<K, V>;
    }

    Internal record of string→{key,value}.

    • [p: string]: HashMapStoreItem<K, V>

    Time O(1), Space O(1)

  • get toEntryFn(): undefined | ((rawElement: R) => [K, V])
  • Get the raw→entry converter function if present.

    Returns undefined | ((rawElement: R) => [K, V])

    Converter function or undefined.

    Time O(1), Space O(1)

Methods

  • (Protected) Create a like-kind instance and seed it from an iterable.

    Type Parameters

    Parameters

    • Optionalentries: Iterable<TR | [TK, TV], any, any> = []

      Iterable used to seed the new map.

    • Optionaloptions: any

      Options forwarded to the constructor.

    Returns any

    A like-kind map instance.

    Time O(N), Space O(N)

  • Delete an entry by key.

    Parameters

    • key: K

      Key to delete.

    Returns boolean

    True if the key was found and removed.

    Time O(1), Space O(1)

  • Test whether all entries satisfy the predicate.

    Parameters

    • predicate: EntryCallback<K, V, boolean>

      (key, value, index, self) => boolean.

    • OptionalthisArg: any

      Optional this for callback.

    Returns boolean

    true if all pass; otherwise false.

    Time O(n), Space O(1)

  • Filter entries into a new map.

    Parameters

    • predicate: EntryCallback<K, V, boolean>

      Predicate (key, value, index, map) → boolean.

    • OptionalthisArg: any

      Value for this inside the predicate.

    Returns any

    A new map containing entries that satisfied the predicate.

    Time O(N), Space O(N)

  • Find the first entry that matches a predicate.

    Parameters

    • callbackfn: EntryCallback<K, V, boolean>

      (key, value, index, self) => boolean.

    • OptionalthisArg: any

      Optional this for callback.

    Returns undefined | [K, V]

    Matching [key, value] or undefined.

    Time O(n), Space O(1)

  • Type guard: check if a raw value is a [key, value] entry.

    Parameters

    • rawElement: any

    Returns rawElement is [K, V]

    True if the value is a 2-tuple.

    Time O(1), Space O(1)

  • Map values to a new map with the same keys.

    Type Parameters

    • VM

    Parameters

    • callbackfn: EntryCallback<K, V, VM>

      Mapping function (key, value, index, map) → newValue.

    • OptionalthisArg: any

      Value for this inside the callback.

    Returns any

    A new map with transformed values.

    Time O(N), Space O(N)

  • Insert or replace a single entry.

    Parameters

    • key: K

      Key.

    • value: V

      Value.

    Returns boolean

    True when the operation succeeds.

    Time O(1), Space O(1)

  • Replace the hash function and rehash the non-object store.

    Parameters

    • fn: ((key: K) => string)

      New hash function for non-object keys.

        • (key): string
        • Parameters

          • key: K

          Returns string

    Returns this

    This map instance.

    Time O(N), Space O(N)

  • Insert many entries from an iterable.

    Parameters

    • entryOrRawElements: Iterable<[K, V] | R, any, any>

      Iterable of entries or raw elements to insert.

    Returns boolean[]

    Array of per-entry results.

    Time O(N), Space O(N)

  • Test whether any entry satisfies the predicate.

    Parameters

    • predicate: EntryCallback<K, V, boolean>

      (key, value, index, self) => boolean.

    • OptionalthisArg: any

      Optional this for callback.

    Returns boolean

    true if any passes; otherwise false.

    Time O(n), Space O(1)