Function called to hash a key before storing or using it to access a value in the map.
Internal data structure that we use to store the entries in the map.
Human readable name for the map instance that is used for debugging purposes.
Protected
Readonly
_Internal data structure that we use to store the entries in the map.
Readonly
hashFunction called to hash a key before storing or using it to access a value in the map.
Readonly
nameHuman readable name for the map instance that is used for debugging purposes.
Static
MissingError thrown when attempting to access a key in the map that does not exist.
Get the iterator over the entires in this map.
The entries in this map as an array of key-value pairs.
Delete an entry from this map by its key, throwing a HashMap.MissingKeyError if the key does not exist in this map.
The key of the entry to delete.
const map = HashMap.fromEntries<number, string>([
[1, "one"],
[2, "two"],
[3, "three"],
]);
map.delete(2);
[...map];
// => [
// [1, "one"],
// [3, "three"],
// ]
map.delete(4); // throws HashMap.MissingKeyError
HashMap.MissingKeyError if the key does not exist in this map.
Delete an entry from this map by its key and returns true
if it exists,
otherwise it simply returns false
.
The key of the entry to delete.
true
if the key existed and the entry was deleted, false
if
the key did not exist.
Returns the entries in this map as an IterableIterator which each
entry as an array of [key, value]
.
An IterableIterator of the entries in this map.
Returns a new map with only the key-value pairs where the given function
returns true
, filtering out those that the given function returns false
for.
Function called for each key-value pair in the map that returns
true
to include the pair in the new map or false
to exclude it.
A new map with only the key-value pairs where the given function
returned true
for.
This function does not mutate the original map instance unless you call a mutating function on the map (4th argument) inside the given function.
Get the value with the given key from this map, throwing a HashMap.MissingKeyError if the key does not exist.
The key of the value to get.
The value with the given key.
Get the value with the given key from this map if it exists, otherwise return the given default value.
The key whose associated value is to be returned.
The value to return if the key is not found.
Checks if this map has an entry with the given key.
The key to check for.
true
if the key exists in this map, false
otherwise.
Get the keys in this map as an IterableIterator.
The keys in this map as an IterableIterator.
Create a new map with entries that are the result of calling the given callback function on each of the key-value pairs in this map.
Function to call on each key-value pair in the map. The result of this function is used as entries in the new map.
A new map with the new entries from calling the given function on each key-value pair in the map.
Create a new map with entries that have keys that are the result of calling the given callback function on each of the key-value pairs in this map.
Function to call on each key-value pair in the map. The result of this function is used as the keys for the entries in the new map.
A new map with the entries with new keys from calling the given function on each key-value pair in the map.
Create a new map with entries that have values that are the result of calling the given callback function on each of the key-value pairs in this map.
Function to call on each key-value pair in the map. The result of this function is used as the values for the entries in the new map.
A new map with the entries with new values from calling the given function on each key-value pair in the map.
Get the values in this map as an IterableIterator.
The values in this map as an IterableIterator.
Static
emptyCreate a new empty map instance.
Options to use to create the new map instance.
A new empty map instance.
This function uses the identity function as the hashing function meaning that keys will be stored without any changes.
Static
emptyCreate a new empty map instance with a custom hashing function.
Options to use to create the new map instance.
A new empty map instance.
Static
fromCreate a new map instance from an array of key-value pairs with a custom hashing function.
Array of key-value pairs to create the map with.
Options to use to create the new map instance.
A new map instance with the given key-value pairs and a given custom hashing function.
const map = HashMap.fromCustomEntries(
[
[new Date("2020-01-01"), 1],
[new Date("2020-02-01"), 2],
[new Date("2020-03-01"), 3],
],
{
hash(key) {
return key.toISOString();
},
});
[...map];
// => [
// ["2020-01-01T00:00:00.000Z", 1],
// ["2020-02-01T00:00:00.000Z", 2],
// ["2020-03-01T00:00:00.000Z", 3],
// ];
Static
fromCreate a new map instance from an array of key-value pairs using the identity as the hash function.
Array of key-value pairs to create the map with.
Options to use to create the new map instance.
A new map instance with the given key-value pairs.
This function uses the identity function as the hashing function
meaning that keys will be stored without any changes which will therefore
use the same behaviour as an ES6 Map
.
Implementation of a key-value map that allows you to customise the way keys are stored by using a custom hashing function.
Remarks
Unlike an ES6
Map
which always uses SameValueZero on the key stored as-is, map allows you to customise the way keys are stored by using a custom hashing function.By default this map uses identity as a hash function meaning that keys are stored as-is, which is the same behaviour as an ES6
Map
.