1.0.7
This HashMap is backed by a Hash array mapped trie.
((Map | HashMap | LinkedHashMap | Iterable<Array<key, value>> | Object)?)
Returns the number of elements in this hashmap.
number
:
the number of elements in the array
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const size = hashmap.size;
console.log(size);
// logs: 3
Returns the number of elements in this hashmap.
number
:
the number of elements in the array
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const length = hashmap.length;
console.log(length);
// logs: 3
Does the map have this key.
key
is in the map.Maps typically index keys, and so is generally a fast operation.
(any)
the matching key we use to identify if we have a match.
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hash and equals methods, rather than them being looked up.
boolean
:
if it holds the key or not.
Does this contain a key that is there
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const hasResult = hashmap.has(1);
// hasResult === true
Does this contain a key that isn't there
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const hasResult = hashmap.has(4);
// hasResult === false
Advanced: using a predefined hashCode and equals on the key
class NameKey {
constructor(firstName, secondName) {
this.firstName = firstName;
this.secondName = secondName;
}
hashCode() {
return (Mootable.hash(firstName) * 31) +Mootable.hash(secondName);
}
equals(other) {
return other && other instanceof NameKey && other.firstName === this.firstName && other.secondName === this.secondName;
}
}
const hashmap = new HashMap([[new NameKey('John','Smith'),'Librarian'],[new NameKey('Orlando','Keleshian'),'Engineer']]);
const key = new NameKey('John','Smith');
const hasResult = hashmap.has(key);
// hasResult === true
Advanced: using a custom hash and equals, to determine if there are entries for a specific hash
const myHash = 3;
const hashEquals = {hash: myHash, equals: () => true}
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const hasResult = hashmap.has(0, hashEquals);
// hasResult === true
// the hash of the number 3 is actually also 3. all 32 bit integers have the same hash.
// 0 doesn't exist in the hashMap, but we are circumventing using the key entirely.
Get a value from the map using this key.
value
from the [key,value]
pair that matches the key.Also provides a way to override both the equals and the hash Performance:
(any)
the matching key we use to identify if we have a match.
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hashcode and equals methods, rather than them being looked up.
any
:
the value of the element that matches.
What is the value for a key
const hashmap = new LinkedHashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const getResult = hashmap.get(1);
// getResult === 'value1'
What is the value for a key that isn't there
const hashmap = new LinkedHashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const getResult = hashmap.get(4);
// getResult === undefined
Advanced: using a predefined hashCode and equals on the key
class NameKey {
constructor(firstName, secondName) {
this.firstName = firstName;
this.secondName = secondName;
}
hashCode() {
return (Mootable.hash(firstName) * 31) +Mootable.hash(secondName);
}
equals(other) {
return other && other instanceof NameKey && other.firstName === this.firstName && other.secondName === this.secondName;
}
}
const hashmap = new HashMap([[new NameKey('John','Smith'),'Librarian'],[new NameKey('Orlando','Keleshian'),'Engineer']]);
const key = new NameKey('John','Smith');
const getResult = hashmap.get(key);
// getResult === 'Librarian'
Advanced: using a custom hash and equals, to get the first entry for a specific hash
const myHash = 3;
const hashEquals = {hash: myHash, equals: () => true}
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const getResult = hashmap.get(0, hashEquals);
// getResult === 'value3'
// the hash of the number 3 is actually also 3. all 32 bit integers have the same hash.
// 0 doesn't exist in the hashMap, but we are circumventing using the key entirely.
Get the key from the map using the provided value. Since values are not hashed, this has to check each value in the map until a value matches, or the whole map, if none match. As such this is a slow operation. Performance O(n) as we have to iterate over the whole map, to find each value and perform an equality against it.
(any)
The value we are searching the map for
(HashMap#overrides<equals>?)
an optional override to allow a user to
define the equals methods, rather than it being looked up on the value.
(any | undefined)
:
the first key for this value or undefined
What is the key for a value
const hashmap = new LinkedHashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const keyOfResult = hashmap.keyOf('value2');
// keyOfResult === 2
What is the value for a key that isn't there
const hashmap = new LinkedHashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const keyOfResult = hashmap.keyOf('value4');
// keyOfResult === undefined
Advanced: using a predefined hashCode and equals on the key
class NameKey {
constructor(firstName, secondName) {
this.firstName = firstName;
this.secondName = secondName;
}
hashCode() {
return (Mootable.hash(firstName) * 31) +Mootable.hash(secondName);
}
equals(other) {
return other && other instanceof NameKey && other.firstName === this.firstName && other.secondName === this.secondName;
}
}
const hashmap = new HashMap([[new NameKey('John','Smith'),'Librarian'],[new NameKey('Orlando','Keleshian'),'Engineer']]);
const keyOfResult = hashmap.keyOf('Engineer');
// getResult ~ NameKey('Orlando','Keleshian')
Advanced: using a custom equals, to get the first key in the hashmap
const myEquals = {equals: () => true}
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const keyOfResult = hashmap.keyOf(0, myEquals);
// keyOfResult === 1
// 0 doesn't exist in the hashMap, but we are circumventing using the key entirely.
Get the key from the map using the provided value, searching the map in reverse. Since values are not hashed, this has to check each value in the map until a value matches, or the whole map, if none match. As such this is a slow operation. Performance O(n) as we have to iterate over the whole map, to find each value and perform an equality against it.
(any)
The value we are searching the map for, (in reverse)
(HashMap#overrides<equals>?)
an optional override to allow a user to
define the equals methods, rather than it being looked up on the value.
(any | undefined)
:
the last key for this value or undefined
What is the key for a value
const hashmap = new LinkedHashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const lastKeyOfResult = hashmap.lastKeyOf('value2');
// lastKeyOfResult === 2
What is the value for a key that isn't there
const hashmap = new LinkedHashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const lastKeyOfResult = hashmap.lastKeyOf('value4');
// lastKeyOfResult === undefined
Advanced: using a predefined hashCode and equals on the key
class NameKey {
constructor(firstName, secondName) {
this.firstName = firstName;
this.secondName = secondName;
}
hashCode() {
return (Mootable.hash(firstName) * 31) +Mootable.hash(secondName);
}
equals(other) {
return other && other instanceof NameKey && other.firstName === this.firstName && other.secondName === this.secondName;
}
}
const hashmap = new HashMap([[new NameKey('John','Smith'),'Librarian'],[new NameKey('Orlando','Keleshian'),'Engineer']]);
const lastKeyOfResult = hashmap.lastKeyOf('Engineer');
// getResult ~ NameKey('Orlando','Keleshian')
Advanced: using a custom equals, to get the last key in the hashmap
const myEquals = {equals: () => true}
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const lastKeyOfResult = hashmap.lastKeyOf(0, myEquals);
// lastKeyOfResult === 3
// 0 doesn't exist in the hashMap, but we are circumventing using the key entirely.
Get the key from the map using the provided value, and wrap it in an Option. Since values are not hashed, this has to check each value in the map until a value matches, or the whole map, if none match. As such this is a slow operation. Performance O(n) as we have to iterate over the whole map, to find each value and perform an equality against it.
(any)
The value we are searching the map for
(HashMap#overrides<equals>?)
an optional overrides to allow a user to
define the equals methods, rather than it being looked up on the value.
Option
:
the first key for this value or none
What is the key for a value
const hashmap = new LinkedHashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalKeyOfResult = hashmap.optionalKeyOf('value2');
// optionalKeyOfResult === Option.some(2)
What is the value for a key that isn't there
const hashmap = new LinkedHashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalKeyOfResult = hashmap.optionalKeyOf('value4');
// optionalKeyOfResult === Option.none
Advanced: using a predefined hashCode and equals on the key
class NameKey {
constructor(firstName, secondName) {
this.firstName = firstName;
this.secondName = secondName;
}
hashCode() {
return (Mootable.hash(firstName) * 31) +Mootable.hash(secondName);
}
equals(other) {
return other && other instanceof NameKey && other.firstName === this.firstName && other.secondName === this.secondName;
}
}
const hashmap = new HashMap([[new NameKey('John','Smith'),'Librarian'],[new NameKey('Orlando','Keleshian'),'Engineer']]);
const optionalKeyOfResult = hashmap.optionalKeyOf('Engineer');
// getResult ~ Option.some(NameKey('Orlando','Keleshian'))
Advanced: using a custom equals, to get the first key in the hashmap
const myEquals = {equals: () => true}
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalKeyOfResult = hashmap.optionalKeyOf(0, myEquals);
// optionalKeyOfResult === Option.some(1)
// 0 doesn't exist in the hashMap, but we are circumventing using the key entirely.
Get the key from the map using the provided value, searching the map in reverse. Since values are not hashed, this has to check each value in the map until a value matches, or the whole map, if none match. As such this is a slow operation. Performance O(n) as we have to iterate over the whole map, to find each value and perform an equality against it.
(any)
The value we are searching the map for, (in reverse)
(HashMap#overrides<equals>?)
an optional overrides to allow a user to
define the equals methods, rather than it being looked up on the value.
Option
:
the last key for this value or none
What is the key for a value
const hashmap = new LinkedHashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalLastKeyOfResult = hashmap.optionalLastKeyOf('value2');
// optionalLastKeyOfResult === Option.some(2)
What is the value for a key that isn't there
const hashmap = new LinkedHashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalLastKeyOfResult = hashmap.optionalLastKeyOf('value4');
// optionalLastKeyOfResult === Option.none
Advanced: using a predefined hashCode and equals on the key
class NameKey {
constructor(firstName, secondName) {
this.firstName = firstName;
this.secondName = secondName;
}
hashCode() {
return (Mootable.hash(firstName) * 31) +Mootable.hash(secondName);
}
equals(other) {
return other && other instanceof NameKey && other.firstName === this.firstName && other.secondName === this.secondName;
}
}
const hashmap = new HashMap([[new NameKey('John','Smith'),'Librarian'],[new NameKey('Orlando','Keleshian'),'Engineer']]);
const optionalLastKeyOfResult = hashmap.optionalLastKeyOf('Engineer');
// getResult ~ Option.some(NameKey('Orlando','Keleshian'))
Advanced: using a custom equals, to get the last key in the hashmap
const myEquals = {equals: () => true}
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalLastKeyOfResult = hashmap.optionalLastKeyOf(0, myEquals);
// optionalLastKeyOfResult === Option.some(3)
// 0 doesn't exist in the hashMap, but we are circumventing using the key entirely.
Get an optional value from the map. This is effectively a more efficent combination of calling has and get at the same time.
some(value)
from the [key,value]
pair that matchesnone()
.Maps typically index keys, and so is generally a fast operation.
(any)
the key we use to identify if we have a match.
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hashcode and equals methods, rather than them being looked up.
Option
:
an optional result.
What is the value for a key
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const getResult = hashmap.optionalGet(1);
// getResult === Option.some('value1') {value:'value1',has:true}
What is the value for a key that isn't there
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const getResult = hashmap.optionalGet(4);
// getResult === Option.none {has:false}
What is the value for a key with an undefined value
const hashmap = new HashMap([[1,'value1'],[2,undefined],[3,'value3']]);
const getResult = hashmap.optionalGet(2);
// getResult === Option.some(undefined) {value:undefined,has:true}
Advanced: using a predefined hashCode and equals on the key
class NameKey {
constructor(firstName, secondName) {
this.firstName = firstName;
this.secondName = secondName;
}
hashCode() {
return (Mootable.hash(firstName) * 31) +Mootable.hash(secondName);
}
equals(other) {
return other && other instanceof NameKey && other.firstName === this.firstName && other.secondName === this.secondName;
}
}
const hashmap = new HashMap([[new NameKey('John','Smith'),'Librarian'],[new NameKey('Orlando','Keleshian'),'Engineer']]);
const key = new NameKey('John','Smith');
const getResult = hashmap.optionalGet(key);
// getResult === Option.some('Librarian') {value:'Librarian',has:true}
Advanced: using a custom hash and equals, to get the first entry for a specific hash
const myHash = 3;
const hashEquals = {hash: myHash, equals: () => true}
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const getResult = hashmap.optionalGet(0, hashEquals);
// getResult === Option.some('value3') {value:'value3',has:true}
// the hash of the number 3 is actually also 3. all 32 bit integers have the same hash.
// 0 doesn't exist in the hashMap, but we are circumventing using the key entirely.
Find the first value in the map which passes the provided MatchesPredicate
.
value
from the [key,value]
pair that matches(HashMap#MatchesPredicate
= (value,key,iterable)=>value
)
the predicate to identify if we have a match.
(any?
= undefined
)
Value to use as
this
when executing
findPredicate
any
:
the value of the element that matches.
Find a value
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const findResult = hashmap.find((value) => value.startsWith('val'));
// findResult === 'value1'
Can't find a value
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const findResult = hashmap.find((value) => value.startsWith('something'));
// findResult === undefined
Find the last value in the map which passes the provided MatchesPredicate
.
value
from the [key,value]
pair that matchesitem that matches)
(HashMap#MatchesPredicate
= (value,key,iterable)=>value
)
the predicate to identify if we have a match.
(any?
= undefined
)
Value to use as
this
when executing
findPredicate
any
:
the value of the element that matches.
Find the last value
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const findLastResult = hashmap.findLast((value) => value.startsWith('val'));
// findLastResult === 'value3'
Can't find a value
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const findLastResult = hashmap.findLast((value) => value.startsWith('something'));
// findLastResult === undefined
Find the first value in the map which passes the provided MatchesPredicate
.
value
from the [key,value]
pair that matches, wrapped in an Option(HashMap#MatchesPredicate
= (value,key,iterable)=>value
)
the predicate to identify if we have a match.
(any?
= undefined
)
Value to use as
this
when executing
findPredicate
(Option<any> | Option.none)
:
the value of the element that matches.
Find a value
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalFindResult = hashmap.optionalFind((value) => value.startsWith('val'));
// optionalFindResult.value === 'value1'
// optionalFindResult.has === true
Can't find a value
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalFindResult = hashmap.optionalFind((value) => value.startsWith('something'));
// optionalFindResult.has === false
Find the last value in the map which passes the provided MatchesPredicate
.
value
from the [key,value]
pair that matches, wrapped in an Optionitem that matches)
(HashMap#MatchesPredicate
= (value,key,iterable)=>value
)
the predicate to identify if we have a match.
(any?
= undefined
)
Value to use as
this
when executing
findPredicate
(Option<any> | Option.none)
:
the value of the element that matches.
Find a value
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalFindLastResult = hashmap.optionalFindLast((value) => value.startsWith('val'));
// optionalFindLastResult.value === 'value3'
// optionalFindLastResult.has === true
Can't find a value
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalFindLastResult = hashmap.optionalFindLast((value) => value.startsWith('something'));
// optionalFindLastResult.has === false
Find the first key in the map which passes the provided MatchesPredicate
.
key
from the [key,value]
pair that matches(HashMap#MatchesPredicate
= (value,key,iterable)=>key
)
the predicate to identify if we have a match.
(any?
= undefined
)
Value to use as
this
when executing
findKeyPredicate
any
:
the key of the element that matches..
Find a key
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const findKeyResult = hashmap.findKey((value) => value.startsWith('val'));
// findKeyResult === 1
Can't find a key
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const findKeyResult = hashmap.findKey((value) => value.startsWith('something'));
// findKeyResult === undefined
Find the last key in the map which passes the provided MatchesPredicate
.
key
from the [key,value]
pair that matchesitem that matches)
(HashMap#MatchesPredicate
= (value,key,iterable)=>key
)
the predicate to identify if we have a match.
(any?
= undefined
)
Value to use as
this
when executing
findKeyPredicate
any
:
the key of the element that matches..
Find a key
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const findLastKeyResult = hashmap.findLastKey((value) => value.startsWith('val'));
// findLastKeyResult === 3
Can't find a key
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const findLastKeyResult = hashmap.findLastKey((value) => value.startsWith('something'));
// findLastKeyResult === undefined
Find the first key in the map which passes the provided MatchesPredicate
.
key
from the [key,value]
pair that matches, wrapped in an Option(HashMap#MatchesPredicate
= (value,key,iterable)=>key
)
the predicate to identify if we have a match.
(any?
= undefined
)
Value to use as
this
when executing
findKeyPredicate
(Option<any> | Option.none)
:
the key of the element that matches.
Find a key
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalFindKeyResult = hashmap.optionalFindKey((value) => value.startsWith('val'));
// optionalFindKeyResult.value === 1
// optionalFindKeyResult.has === true
Can't find a key
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalFindKeyResult = hashmap.optionalFindKey((value) => value.startsWith('something'));
// optionalFindKeyResult.has === false
Find the last key in the map which passes the provided MatchesPredicate
.
key
from the [key,value]
pair that matches, wrapped in an Optionitem that matches)
(HashMap#MatchesPredicate
= (value,key,iterable)=>key
)
the predicate to identify if we have a match.
(any?
= undefined
)
Value to use as
this
when executing
findKeyPredicate
(Option<any> | Option.none)
:
the key of the element that matches.
Find a key
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalFindLastKeyResult = hashmap.optionalFindLastKey(value) => value.startsWith('val'));
// optionalFindLastKeyResult.value === 3
// optionalFindLastKeyResult.has === true
Can't find a key
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const optionalFindLastKeyResult = hashmap.optionalFindLastKey((value) => value.startsWith('something'));
// optionalFindLastKeyResult.has === false
Sets a value onto this map, using the key as its reference.
(any)
the key we want to key our value to
(any)
the value we are setting
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hashcode and equals methods, rather than them being looked up.
HashMap
:
this hashmap
set a value
const hashmap = new HashMap();
hashmap.set(1,'value1');
const hasResult = hashmap.has(1);
// hasResult === true
overwrite a value
const hashmap = new HashMap([[1,'value1'],[2,'value2']]);
hashmap.set(2,'other');
const getResult = hashmap.get(2);
// getResult === 'other'
Advanced: using a predefined hashCode and equals on the key
class NameKey {
constructor(firstName, secondName) {
this.firstName = firstName;
this.secondName = secondName;
}
hashCode() {
return (Mootable.hash(firstName) * 31) +Mootable.hash(secondName);
}
equals(other) {
return other && other instanceof NameKey && other.firstName === this.firstName && other.secondName === this.secondName;
}
}
const hashmap = new HashMap();
hashmap.set(new NameKey('John','Smith'),'Librarian');
const hasResult = hashmap.has(new NameKey('John','Smith'));
// hasResult === true
Advanced: using a custom hash and equals, to set a value to a specific hash
const hashmap = new HashMap();
hashmap.set(1,'value1', {hash: 3});
const hasResult = hashmap.has(3, {equals: () => true} );
// hasResult === true
// the hash of the number 3 is actually also 3. all 32 bit integers have the same hash.
// 0 doesn't exist in the hashMap, but we are circumventing using the key entirely.
Given a key and a handler object, the emplace method will either remap an existing entry, insert a new entry from a mapping function, or both. emplace will return the updated or inserted value.
(any)
the key we want to key our value to
(HashMap#emplaceHandler<insert, update>)
the insert and update methods we
want to use.
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hashcode and equals methods, rather than them being looked up.
any
:
the new value that was set, or overwrote.
insert into the map
const hashmap = new HashMap();
const handler = {
update: () => {
return 'update';
},
insert: (key, map) => {
return 'insert';
}
};
const ret = hashmap.emplace('key', handler)
// hashmap = [['key', 'insert']]
// ret === 'insert'
update the map
const hashmap = new HashMap([['key','value']]);
const handler = {
update: () => {
return 'update';
},
insert: (key, map) => {
return 'insert';
}
};
const ret = hashmap.emplace('key', handler)
// hashmap = [['key', 'update']]
// ret === 'update'
insert into the map if the key already exists without an update
const hashmap = new HashMap([['key','value']]);
const handler = {
insert: (key, map) => {
return 'insert';
}
};
const ret = hashmap.emplace('key', handler)
// hashmap = [['key', 'value']]
// ret === 'value'
update into the map without an insert method (throws an error)
const hashmap = new HashMap([['key','value']]);
const handler = {
update: (oldValue, key, map) => {
return 'update';
}
};
hashmap.emplace('key', handler)
// throws an Error as insert doesn't exist
// hashmap = [['key', 'value']]
Advanced: using a predefined hashCode and equals on the key
class NameKey {
constructor(firstName, secondName) {
this.firstName = firstName;
this.secondName = secondName;
}
hashCode() {
return (Mootable.hash(firstName) * 31) +Mootable.hash(secondName);
}
equals(other) {
return other && other instanceof NameKey && other.firstName === this.firstName && other.secondName === this.secondName;
}
}
const handler = {
insert: (key, map) => {
return 'Librarian';
}
};
const hashmap = new HashMap();
const ret = hashmap.emplace(new NameKey('John','Smith'),handler);
// ret === 'Librarian'
Advanced: using a custom hash and equals, to emplace a value to a specific hash
const handler = {
insert: (key, map) => {
return 'value1';
}
};
const hashmap = new HashMap();
const ret = hashmap.emplace(1,handler, {hash: 3});
// ret === 'value1'
// the hash of the number 3 is actually also 3. all 32 bit integers have the same hash.
// 0 doesn't exist in the hashMap, but we are circumventing using the key entirely.
Copies all the entries from the map, array or iterable, into this hashmap.
((Map | HashMap | LinkedHashMap | Iterable<Array<key, value>> | Object))
the
iterable to copy
HashMap
:
this hashmap, with the values copied to it.
copy into the HashMap from an array of key value pairs
const hashmap = new HashMap([['key0','value0']]);
const arr = [[1,'value1'],[2,'value2'],[3,'value3']];
hashmap.copy(arr);
// hashmap.size === 4;
copy into the HashMap from another map
const hashmap = new HashMap([['key0','value0']]);
const map = new Map([[1,'value1'],[2,'value2'],[3,'value3']])
hashmap.copy(map);
// hashmap.size === 4;
copy into the HashMap from another HashMap
const first = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']])
const hashmap = new HashMap(first);
// hashmap.size === 3;
copy into the HashMap from a class with symbol iterator
const hashmap = new HashMap([['key0','value0']]);
class MyIterable = {
*[Symbol.iterator] () {
yield ["key1", "value1"];
yield ["key2", "value2"];
yield ["key3", "value3"];
yield ["key4", "value4"];
}
}
const iterable = new MyIterable();
hashmap.copy(iterable);
// hashmap.size === 5;
// it doesn't have to be a generator, an iterator works too.
copy into the HashMap from an object with an entries generator function
const hashmap = new HashMap([['key0','value0']]);
const entriesObj = {
entries: function* () {
yield ["key1", "value1"];
yield ["key2", "value2"];
yield ["key3", "value3"];
yield ["key4", "value4"];
}
}
hashmap.copy(entriesObj);
// hashmap.size === 5;
// it doesn't have to be a generator, an iterator works too.
copy into the HashMap from an object with a forEach function
const hashmap = new HashMap([['key0','value0']]);
const forEachObj = {
forEach: (callback, ctx) => {
for (let i = 1; i <= 4; i++) {
callback.call(ctx, 'value' + i, 'key' + i);
}
}
};
hashmap.copy(forEachObj);
// hashmap.size === 5;
Deletes an entry from this hashmap, using the provided key
(any)
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hashcode and equals methods, rather than them being looked up. * @return {HashMap}
Execute the provided callback on every [key,value]
pair of this map iterable.
(HashMap#ForEachCallback
= (value,key,map)=>{}
)
(any?)
Value to use as
this
when executing
forEachCallback
HashMap
:
the hashmap you are foreaching on..
Log all the keys and values.
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
mapIterable.forEach((value) => console.log(key, value));
// will log to the console:
// 1 value1
// 2 value2
// 3 value3
// Ordering is deterministic on paper, but from a usability point of view effectively random
// as it is ordered by a mix of the hash of the key, and order of insertion.
Execute the provided callback on every [key,value]
pair of this map iterable in reverse.
(HashMap#ForEachCallback
= (value,key,map)=>{}
)
(any?)
Value to use as
this
when executing
forEachCallback
HashMap
:
the hashmap you are foreaching on..
Log all the keys and values.
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
mapIterable.forEachRight((value) => console.log(key, value));
// will log to the console:
// 3 value3
// 2 value2
// 1 value1
// Ordering is deterministic on paper, but from a usability point of view effectively random
// as it is ordered by a mix of the hash of the key, and order of insertion.
Test to see if ALL elements pass the test implemented by the passed MatchesPredicate
.
(HashMap#MatchesPredicate
= (value,key,iterable)=>true
)
if the provided function returns
false
, at any point the
every()
function returns false.
(any?
= undefined
)
Value to use as
this
when executing
everyPredicate
(HashMap#overrides<reverse>?
= undefined
)
a set of optional overrides to allow a user to define whether to search in reverse
boolean
:
true if all elements match, false if one or more elements fails to match.
Do all values start with 'value'. (yes)
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const everyResult = hashmap.every((value) => value.startsWith('value'));
// everyResult === true
Do all values start with value. (no)
const hashmap = new HashMap([[1,'value1'],[2,'doesntStart'],[3,'value3']]);
const everyResult = hashmap.every((value) => value.startsWith('value'));
// everyResult === false
Test to see if ANY element pass the test implemented by the passed MatchesPredicate
.
(HashMap#MatchesPredicate
= (value,key,iterable)=>true
)
the predicate to identify if we have a match.
(any?
= undefined
)
Value to use as
this
when executing
somePredicate
(HashMap#overrides<reverse>?
= undefined
)
a set of optional overrides to allow a user to define whether to search in reverse
boolean
:
true if all elements match, false if one or more elements fails to match.
Do any values start with value. (yes all of them)
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const someResult = hashmap.some((value) => value.startsWith('value'));
// someResult === true
Do any values start with value. (yes 2 of them)
const hashmap = new HashMap([[1,'value1'],[2,'doesntStart'],[3,'value3']]);
const someResult = hashmap.some((value) => value.startsWith('value'));
// someResult === true
Iterate through the map reducing it to a single value.
(HashMap#ReduceFunction)
the predicate to identify if we have a match.
(any?)
the initial value to start on the reduce.
(any?)
Value to use as
this
when executing
reduceFunction
any
:
the final accumulated value.
undefined
or
null
, unlike Array.reduce,
no error occurs, and it is simply passed as the accumulator value
add all the keys
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const reduceResult = hashmap.reduce((accumulator, value, key) => accumulator+key, 0);
// reduceResult === 6
add all the values into one string in reverse order
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const reduceResult = hashmap.reduce((accumulator, value) => value+accumulator, '');
// reduceResult === 'value3value2value1'
Iterate backwards through the map reducing it to a single value.
(HashMap#ReduceFunction)
the predicate to identify if we have a match.
(any?)
the initial value to start on the reduce.
(any?)
Value to use as
this
when executing
reduceFunction
any
:
the final accumulated value.
undefined
or
null
, unlike Array.reduceRight,
no error occurs, and it is simply passed as the accumulator value
add all the keys
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const reduceResult = hashmap.reduceRight((accumulator, value, key) => accumulator+key, 0);
// reduceResult === 6
add all the values into one string in reverse order
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
const reduceResult = hashmap.reduceRight((accumulator, value) => value+accumulator, '');
// reduceResult === 'value1value2value3'
Iterates over all the entries in the map.
iterate over the map
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
for ([key, value] of hashmap) {
console.log(key,value);
}
// logs:
// 1 value1
// 2 value2
// 3 value3
Iterates over all the entries in the map.
iterate over the map
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
for ([key, value] of hashmap.entries()) {
console.log(key,value);
}
// logs:
// 1 value1
// 2 value2
// 3 value3
Iterates over all the entries in the map in reverse.
iterate over the map in reverse
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
for ([key, value] of hashmap.entriesRight()) {
console.log(key,value);
}
// logs:
// 3 value3
// 2 value2
// 1 value1
Iterates over all the keys in the map.
iterate over the keys
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
for (const key of hashmap.keys()) {
console.log(key);
}
// logs:
// 1
// 2
// 3
Iterates over all the values in the map.
iterate over the values
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
for (const value of hashmap.values()) {
console.log(value);
}
// logs:
// value1
// value2
// value3
Iterates over all the keys in the map in reverse.
iterate over the keys
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
for (const key of hashmap.keysRight()) {
console.log(key);
}
// logs:
// 3
// 2
// 1
Iterates over all the values in the map in reverse.
iterate over the values
const hashmap = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']]);
for (const value of hashmap.valuesRight()) {
console.log(value);
}
// logs:
// value3
// value2
// value1
HashMap - LinkedHashMap Implementation for JavaScript
Extends HashMap
((Map | HashMap | LinkedHashMap | Iterable<Array<key, value>> | Object)?)
(any)
(any)
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hashcode and equals methods, rather than them being looked up. * @return {HashMap}
LinkedHashMap
:
(any)
(any)
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hashcode and equals methods, rather than them being looked up. * @return {HashMap}
any
:
(any)
(any)
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hashcode and equals methods, rather than them being looked up. * @return {HashMap}
LinkedHashMap
:
(any)
(any)
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hashcode and equals methods, rather than them being looked up. * @return {HashMap}
any
:
(any)
(any)
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hashcode and equals methods, rather than them being looked up. * @return {HashMap}
LinkedHashMap
:
(any)
(any)
(HashMap#overrides<equals, hash>?)
a set of optional overrides to allow a user to define the hashcode and equals methods, rather than them being looked up. * @return {HashMap}
any
:
LinkedHashMap
:
Makes a copy of this LinkedHashMap
LinkedHashMap
:
Iterates over all the entries in the map.
Iterates over all the entries in the map.
Iterates over all the entries in the map in reverse order.
Iterates over all the keys in the map.
Iterates over all the values in the map.
Iterates over all the keys in the map in reverse.
Iterates over all the values in the map in reverse.
Option - a class to get round nullable fields.
(any)
whether it contains a value or not.
(any)
the value to set
iterating over some
const opt = Option.some("hello");
for (value of opt) {
// loops once.
console.log(opt);
}
console.log("world");
// logs - hello\nworld
iterating over none
const opt = Option.none;
for (value of opt) {
// does not loop.
console.log(opt);
}
console.log("world");
// logs - world
A constant representation of an Option with nothing in it:
{value:undefined,has:false}
Type: Option
create an option using none
const option = Option.none;
// option.has === false
// option.value === undefined
// option.size === 0
When called with a value returns an Option object of the form:
{value:value,has:true}
Even if a value is not provided it still counts as existing, this is different from other libraries,
we are effectively saying, null and undefined count as valid values.
(any)
the value
Option
:
the option in the form
{value:value,has:true}
create an option using some
const myValue = 'hello';
const option = Option.some(myValue);
// option.has === true
// option.value === 'hello'
// option.size === 1
Provides an iterable for the Option If using a for loop.
Generator<any, void, any>
:
iterating over some
const opt = Option.some("hello");
for (value of opt) {
// loops once.
console.log(opt);
}
console.log("world");
// logs - hello\nworld
iterating over none
const opt = Option.none;
for (value of opt) {
// does not loop.
console.log(opt);
}
console.log("world");
// logs - world
User defined hashing and equals methods HashMap will find the best fit for your objects, and if your keys themselves have the appropriate methods, then it will use them. However if you want to override that functionality this object allows you to do it. Not all functions and properties are used in every function, please refer to that function for details. If a function in future chooses to use one of the other properties or functions, it will NOT be marked as a breaking change. So be explicit.
Type: Object
((number | HashMap#overrideHash)?)
: The overriding hash value, or method to use.
(HashMap#overrideEquals?)
: The overriding equals method to use
(boolean?)
: whether to search in reverse.
Emplace handler methods
Type: Object
(HashMap#emplaceUpdate?)
: The update method to use.
(HashMap#emplaceInsert?)
: The insert method to use
Test each element of the map to see if it matches and return
Type: Function
boolean
:
a value that coerces to true if it matches, or to false otherwise.
Only match keys divisible by 2
const myMatchPredicate = (value, key) => key % 2 === 0;
Only match values which are equal to another key in the map
const myMatchPredicate = (value, key, mapIterable) => mapIterable.has(value);
An alternative implementation, (but potentially slower, and assumes no undefined value)
const myMatchPredicate = (value, key, mapIterable) => mapIterable.indexOf(key) !== undefined;
Reduce Function
A callback to accumulate values from the HashMap [key,value]
into a single value.
Type: Function
(any?)
the value from the last execution of this function.
(any?)
the entry value.
(any?)
the entry key
(HashMap?)
the calling HashMap.
any
:
[accumulator] - the value to pass to the next time this function is called or the final return value.
add all the keys
const reduceFunction = (accumulator, value, key) => accumulator+key
Is the passed value not null and a function
((function | any))
the function/object to test
boolean
:
true if this is function and not null.
test if its a function
const myFunc = () => 1 + 1;
Mootable.isFunction(myFunc) === true;
test if its not a function
const notAFunction = {};
Mootable.isFunction(notAFunction) === false;
test if its null
const notAFunction = null;
Mootable.isFunction(notAFunction) === false;
Is the passed object iterable and not null, i.e. it has a function that has a type of [Symbol.iterator]
((Iterable | any))
the object to test
boolean
:
true if this has a Symbol.iterator function
test if its iterable
class MyIterable {
* [Symbol.iterator]() {
yield 1;
}
}
Mootable.isIterable(new MyIterable()) === true;
test if its not an iterable
const notAnIterable = {};
Mootable.isIterable(notAnIterable) === false;
test if its null
const notAnIterable = null;
Mootable.isIterable(notAnIterable) === false;
Is the passed value is not null and is a string
((string | any))
the string/object to test
boolean
:
true if this is a string
test if its iterable
const myString = "hello world";
Mootable.isString(myString) === true;
test if its not an iterable
const notAString = {};
Mootable.isString(notAString) === false;
test if its null
const notAString = null;
Mootable.isString(notAString) === false;
sameValue is the result of Object.is. The only difference between sameValue and sameValueZero is that +0 and -0 are considered different with sameValue.
(any)
the first object to compare
(any)
the second object to compare
boolean
:
if they are equals according to
ECMA Spec for Same Value
sameValueZero is the equality method used by Map, Array, Set etc. The only difference between === and sameValueZero is that NaN counts as equal on sameValueZero
(any)
the first object to compare
(any)
the second object to compare
boolean
:
if they are equals according to
ECMA Spec for Same Value Zero
The abstract Equals method ==
.
Simply does an abstract equality comparison ==
against 2 values
(any)
the first object to compare
(any)
the second object to compare
boolean
:
if they are equals according to
ECMA Spec for Abstract Equality
The strict Equals method ===
.
Simply does a strict equality comparison ===
against 2 values
(any)
the first object to compare
(any)
the second object to compare
boolean
:
if they are equals according to
ECMA Spec for Strict Equality
Counts the number of ones in a binary representation of a 32 bit integer.
(number)
32 bit integer
number
:
amount of ones.
count the number of bits set to one for the value 22
const myNumber = 22; // 10110 in binary
Mootable.hammingWeight(myNumber) === 3;
count the number of bits set to one for the value 12947
const myNumber = 12947; // 11001010010011 in binary
Mootable.hammingWeight(myNumber) === 7;
Modified Murmur3 hash generator, with capped lengths. This is NOT a cryptographic hash, this hash is designed to create as even a spread across a 32bit integer as is possible.
(any)
the string being hashed
(any
= 0
)
the max limit on the number of characters to hash
(any
= 0
)
an optional random seed, or previous hash value to continue hashing against.
number
:
the hash
Given any object return back a hashcode
Otherwise
As with all hashmaps, there is a contractual equivalence between hashcode and equals methods, in that any object that equals another, should produce the same hashcode.
(any)
the key to get the hash code from
number
:
the hash code.
Given a key, produce an equals method that fits the hashcode contract.
key.equals(other)
, or key.equals(other,key)
in the case of static-like functions.The expectation and requirement is this key will always be the first argument to the method, the behaviour maybe unexpected if parameters are reversed.
As with all hashmaps, there is a contractual equivalence between hashcode and equals methods, in that any object that equals another, should produce the same hashcode.
(any)
the key to get the hash code from
(function (any, any): boolean)
:
an equals function for 2 keys.
Given any object return back a hashcode
Otherwise
As with all hashmaps, there is a contractual equivalence between hashcode and equals methods, in that any object that equals another, should produce the same hashcode.
(any)
the key to get the hash code from
(any)
{hash: number, equals: function}
:
the hash code and equals function.
A function that when called with a value returns an Option object of the form:
{value:value,has:true}
Even if a value is not provided it still counts as existing, this is different from other libraries,
we are effectively saying that null and undefined count as valid values.
Type: function (any?): Option
(any)
create an option using some
const myValue = 'hello';
const option = some(myValue);
// option.has === true
// option.value === 'hello'
// option.size === 1