HashMap
Defined in: src/map/HashMap.ts:132
A hash table Map: the default map. Keys are equal by equals()/hashCode() (SameValueZero for
primitives and objects without equals()); null and undefined keys and values are allowed. Iteration order is
unspecified. get, put, containsKey and remove run in O(1) expected time (O(k) when k keys share one
hashCode()); containsValue is O(n). Iterators and views are fail-fast.
Storage is hybrid: small non-negative integer keys live in a dense array indexed by the key (faster than any hash); other keys whose equality is SameValueZero (primitives, plain objects) live in a native Map; keys with equals()/hashCode() and Dates live in a bucket table keyed by hashCode(). An integer key that would make the array sparse goes to the native Map instead, so lookups of index keys check the array, then the native Map. A key object whose equals() claims equality with a primitive is still a distinct key from that primitive (equals must be symmetric, and a primitive never equals an object).
Extension hooks (for LinkedHashMap and friends):
- newNode(key, value): creates every node; override to return a HashNode subclass.
- afterNodeInsertion(node): after a new mapping was added (modCount already bumped).
- afterNodeAccess(node): on a hit by get/getOrDefault, put/putIfAbsent on an existing key, replace that matched, and computeIfAbsent/computeIfPresent/compute/merge on an existing key (when they keep or update it). Not fired by containsKey, entry.setValue or replaceAll.
- afterNodeRemoval(node): after a mapping was removed (not fired by clear(): override clear()).
- newNodeIterator(): the cursor behind every view iterator and [Symbol.iterator].
- forEachNode(action): the traversal behind forEach, replaceAll, view forEach/toArray and copies. Subclasses that keep their own order override newNodeIterator, forEachNode, clear and clone; removeNode(node) is the way to delete a node from such a subclass iterator.
Extends
Section titled “Extends”AbstractMap<K,V>
Extended by
Section titled “Extended by”Type Parameters
Section titled “Type Parameters”K
V
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new HashMap<
K,V>():HashMap<K,V>
Defined in: src/map/HashMap.ts:152
Creates an empty map.
Returns
Section titled “Returns”HashMap<K, V>
Overrides
Section titled “Overrides”Constructor
Section titled “Constructor”new HashMap<
K,V>(initialCapacity,loadFactor?):HashMap<K,V>
Defined in: src/map/HashMap.ts:157
Creates an empty map. The arguments are sizing hints only: they are validated but storage grows on demand.
Parameters
Section titled “Parameters”initialCapacity
Section titled “initialCapacity”number
loadFactor?
Section titled “loadFactor?”number
Returns
Section titled “Returns”HashMap<K, V>
Throws
Section titled “Throws”IllegalArgumentException if initialCapacity is negative or NaN, or loadFactor is not positive
Overrides
Section titled “Overrides”AbstractMap<K, V>.constructor
Constructor
Section titled “Constructor”new HashMap<
K,V>(map):HashMap<K,V>
Defined in: src/map/HashMap.ts:162
Creates a map with the same mappings as map.
Parameters
Section titled “Parameters”Map<K, V>
Returns
Section titled “Returns”HashMap<K, V>
Throws
Section titled “Throws”NullPointerException if map is null or undefined
Overrides
Section titled “Overrides”AbstractMap<K, V>.constructor
Properties
Section titled “Properties”modCount
Section titled “modCount”modCount:
number=0
Defined in: src/map/HashMap.ts:144
Structural modification count (new key, removal, clear); iterators compare against it.
Methods
Section titled “Methods”[INSPECT]()
Section titled “[INSPECT]()”[INSPECT]():
string
Defined in: src/abstract/AbstractMap.ts:187
Node’s util.inspect hook: console.log prints the toString() form.
Returns
Section titled “Returns”string
Inherited from
Section titled “Inherited from”[iterator]()
Section titled “[iterator]()”[iterator]():
IterableIterator<[K,V]>
Defined in: src/map/HashMap.ts:567
Returns a fail-fast iterator over the mappings as [key, value] pairs, in the same unspecified order as
entrySet().
Returns
Section titled “Returns”IterableIterator<[K, V]>
Overrides
Section titled “Overrides”clear()
Section titled “clear()”clear():
void
Defined in: src/map/HashMap.ts:529
Removes all mappings. Open iterators then fail with ConcurrentModificationException.
Returns
Section titled “Returns”void
Overrides
Section titled “Overrides”clone()
Section titled “clone()”clone():
HashMap<K,V>
Defined in: src/map/HashMap.ts:186
Shallow copy (keys and values are not cloned) of the same runtime class, subclass fields included.
Returns
Section titled “Returns”HashMap<K, V>
compute()
Section titled “compute()”compute(
key,fn):V|null
Defined in: src/map/HashMap.ts:728
Stores fn(key, current), where current is the mapped value or null; a null/undefined result removes the
mapping (or leaves the key absent). O(1) expected plus the cost of fn.
Parameters
Section titled “Parameters”K
(key, value) => V | null | undefined
Returns
Section titled “Returns”V | null
the new value, or null if there is now no mapping
Throws
Section titled “Throws”NullPointerException if fn is null
Throws
Section titled “Throws”ConcurrentModificationException if fn structurally modifies this map
Overrides
Section titled “Overrides”computeIfAbsent()
Section titled “computeIfAbsent()”computeIfAbsent(
key,fn):V|null
Defined in: src/map/HashMap.ts:682
If key is absent or mapped to null, stores fn(key) unless that is null/undefined (then nothing is
stored). O(1) expected plus the cost of fn.
Parameters
Section titled “Parameters”K
(key) => V | null | undefined
Returns
Section titled “Returns”V | null
the existing non-null value, the newly stored value, or null
Throws
Section titled “Throws”NullPointerException if fn is null
Throws
Section titled “Throws”ConcurrentModificationException if fn structurally modifies this map
Overrides
Section titled “Overrides”computeIfPresent()
Section titled “computeIfPresent()”computeIfPresent(
key,fn):V|null
Defined in: src/map/HashMap.ts:705
If key has a non-null value, replaces it with fn(key, value); a null/undefined result removes the mapping.
O(1) expected plus the cost of fn.
Parameters
Section titled “Parameters”K
(key, value) => V | null | undefined
Returns
Section titled “Returns”V | null
the new value, or null if the key was absent or has been removed
Throws
Section titled “Throws”NullPointerException if fn is null
Throws
Section titled “Throws”ConcurrentModificationException if fn structurally modifies this map
Overrides
Section titled “Overrides”containsKey()
Section titled “containsKey()”containsKey(
key):boolean
Defined in: src/map/HashMap.ts:419
Returns true if this map has a mapping for key, even if it maps to null. O(1) expected.
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”boolean
Overrides
Section titled “Overrides”containsValue()
Section titled “containsValue()”containsValue(
value):boolean
Defined in: src/map/HashMap.ts:424
Returns true if one or more keys map to a value equal to value. O(n): scans every value with equals().
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”boolean
Overrides
Section titled “Overrides”entrySet()
Section titled “entrySet()”entrySet():
Set<MapEntry<K,V>>
Defined in: src/map/HashMap.ts:558
Returns a live view of the mappings. Its entries are the map’s own nodes, so setValue writes through; removing
from it removes the mapping, and it does not support add (UnsupportedOperationException).
Returns
Section titled “Returns”Set<MapEntry<K, V>>
Overrides
Section titled “Overrides”equals()
Section titled “equals()”equals(
o):boolean
Defined in: src/abstract/AbstractMap.ts:158
Returns true if o is a Map with the same mappings (any Map implementation). A
ClassCastException or NullPointerException from o’s lookups counts as “not equal”.
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”boolean
Inherited from
Section titled “Inherited from”forEach()
Section titled “forEach()”forEach(
action):void
Defined in: src/map/HashMap.ts:615
Calls action for each mapping, in iteration order.
Parameters
Section titled “Parameters”action
Section titled “action”(key, value) => void
Returns
Section titled “Returns”void
Throws
Section titled “Throws”NullPointerException if action is null
Throws
Section titled “Throws”ConcurrentModificationException if action structurally modifies this map
Overrides
Section titled “Overrides”get(
key):V|null
Defined in: src/map/HashMap.ts:405
Returns the value mapped to key, or null if there is none (or the key maps to null). O(1) expected.
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”V | null
Overrides
Section titled “Overrides”getNode()
Section titled “getNode()”getNode(
key):HashNode<K,V> |undefined
Defined in: src/map/HashMap.ts:280
Returns the node for key, or undefined. Fires no hook.
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”HashNode<K, V> | undefined
getOrDefault()
Section titled “getOrDefault()”Call Signature
Section titled “Call Signature”getOrDefault(
key,defaultValue):V
Defined in: src/map/HashMap.ts:596
Returns the value mapped to key, or defaultValue if there is no mapping. A key mapped to null returns null.
O(1) expected.
Parameters
Section titled “Parameters”unknown
defaultValue
Section titled “defaultValue”V
Returns
Section titled “Returns”V
Overrides
Section titled “Overrides”Call Signature
Section titled “Call Signature”getOrDefault(
key,defaultValue):V|null
Defined in: src/map/HashMap.ts:601
Returns the value mapped to key, or defaultValue (here possibly null) if there is no mapping. O(1) expected.
Parameters
Section titled “Parameters”unknown
defaultValue
Section titled “defaultValue”V | null
Returns
Section titled “Returns”V | null
Overrides
Section titled “Overrides”hashCode()
Section titled “hashCode()”hashCode():
number
Defined in: src/map/HashMap.ts:584
Returns the sum over all entries of hash(key) ^ hash(value), in 32-bit arithmetic. O(n).
Returns
Section titled “Returns”number
Overrides
Section titled “Overrides”isEmpty()
Section titled “isEmpty()”isEmpty():
boolean
Defined in: src/map/HashMap.ts:396
Returns true if this map has no mappings. O(1).
Returns
Section titled “Returns”boolean
Overrides
Section titled “Overrides”keySet()
Section titled “keySet()”keySet():
Set<K>
Defined in: src/map/HashMap.ts:542
Returns a live view of the keys, in the map’s unspecified order. Removing from it removes the mapping; it does not
support add (UnsupportedOperationException).
Returns
Section titled “Returns”Set<K>
Overrides
Section titled “Overrides”merge()
Section titled “merge()”merge(
key,value,fn):V|null
Defined in: src/map/HashMap.ts:750
If key is absent or mapped to null, maps it to value; otherwise stores fn(oldValue, value), and a
null/undefined result removes the mapping. O(1) expected plus the cost of fn.
Parameters
Section titled “Parameters”K
V
(oldValue, value) => V | null | undefined
Returns
Section titled “Returns”V | null
the new value, or null if the mapping was removed
Throws
Section titled “Throws”NullPointerException if value or fn is null
Throws
Section titled “Throws”ConcurrentModificationException if fn structurally modifies this map
Overrides
Section titled “Overrides”put(
key,value):V|null
Defined in: src/map/HashMap.ts:447
Maps key to value, replacing any previous value. O(1) expected. null and undefined keys and values are
allowed.
Parameters
Section titled “Parameters”K
V
Returns
Section titled “Returns”V | null
the previous value, or null if there was no mapping
Overrides
Section titled “Overrides”putAll()
Section titled “putAll()”putAll(
m):void
Defined in: src/map/HashMap.ts:518
Copies every mapping of m into this map, as put would. O(m) expected for m mappings; putting a map into itself
does nothing.
Parameters
Section titled “Parameters”Map<K, V>
Returns
Section titled “Returns”void
Throws
Section titled “Throws”NullPointerException if m is null
Overrides
Section titled “Overrides”putIfAbsent()
Section titled “putIfAbsent()”putIfAbsent(
key,value):V|null
Defined in: src/map/HashMap.ts:636
Maps key to value if it is absent or mapped to null. O(1) expected.
Parameters
Section titled “Parameters”K
V
Returns
Section titled “Returns”V | null
the current value if it was non-null (and nothing changed), otherwise null
Overrides
Section titled “Overrides”remove()
Section titled “remove()”Call Signature
Section titled “Call Signature”remove(
key):V|null
Defined in: src/map/HashMap.ts:496
Removes the mapping for key, if present. O(1) expected.
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”V | null
the removed value, or null if there was no mapping
Overrides
Section titled “Overrides”Call Signature
Section titled “Call Signature”remove(
key,value):boolean
Defined in: src/map/HashMap.ts:501
Removes the mapping for key only if it is currently mapped to a value equal to value. O(1) expected.
Parameters
Section titled “Parameters”unknown
unknown
Returns
Section titled “Returns”boolean
true if the mapping was removed
Overrides
Section titled “Overrides”removeNode()
Section titled “removeNode()”removeNode(
node):void
Defined in: src/map/HashMap.ts:342
Removes exactly this node (by identity), then calls HashMap.afterNodeRemoval.
Parameters
Section titled “Parameters”HashNode<K, V>
Returns
Section titled “Returns”void
replace()
Section titled “replace()”Call Signature
Section titled “Call Signature”replace(
key,value):V|null
Defined in: src/map/HashMap.ts:653
Replaces the value for key only if key currently has a mapping (even to null). O(1) expected.
Parameters
Section titled “Parameters”K
V
Returns
Section titled “Returns”V | null
the previous value, or null if there was no mapping
Overrides
Section titled “Overrides”Call Signature
Section titled “Call Signature”replace(
key,oldValue,newValue):boolean
Defined in: src/map/HashMap.ts:659
Replaces the value for key with newValue only if it is currently mapped to a value equal to oldValue. O(1)
expected.
Parameters
Section titled “Parameters”K
oldValue
Section titled “oldValue”V
newValue
Section titled “newValue”V
Returns
Section titled “Returns”boolean
true if the value was replaced
Overrides
Section titled “Overrides”replaceAll()
Section titled “replaceAll()”replaceAll(
fn):void
Defined in: src/map/HashMap.ts:625
Replaces each value with fn(key, value), in iteration order. This is not a structural change.
Parameters
Section titled “Parameters”(key, value) => V
Returns
Section titled “Returns”void
Throws
Section titled “Throws”NullPointerException if fn is null
Throws
Section titled “Throws”ConcurrentModificationException if fn structurally modifies this map
Overrides
Section titled “Overrides”size()
Section titled “size()”size():
number
Defined in: src/map/HashMap.ts:391
Returns the number of mappings. O(1).
Returns
Section titled “Returns”number
Overrides
Section titled “Overrides”toString()
Section titled “toString()”toString():
string
Defined in: src/abstract/AbstractMap.ts:191
Returns {k1=v1, k2=v2} in entry-set order; a key or value that is this map prints as (this Map).
Returns
Section titled “Returns”string
Inherited from
Section titled “Inherited from”values()
Section titled “values()”values():
Collection<V>
Defined in: src/map/HashMap.ts:550
Returns a live view of the values. Removing from it removes the mapping; it does not support add
(UnsupportedOperationException).
Returns
Section titled “Returns”Collection<V>
Overrides
Section titled “Overrides”newHashMap()
Section titled “newHashMap()”
staticnewHashMap<K,V>(numMappings):HashMap<K,V>
Defined in: src/map/HashMap.ts:180
Returns an empty map intended for numMappings mappings (a sizing hint only).
Type Parameters
Section titled “Type Parameters”K
V
Parameters
Section titled “Parameters”numMappings
Section titled “numMappings”number
Returns
Section titled “Returns”HashMap<K, V>
Throws
Section titled “Throws”IllegalArgumentException if numMappings is negative or NaN
staticof<K,V>(…pairs):HashMap<K,V>
Defined in: src/map/HashMap.ts:170
Returns a new map of the given [key, value] pairs; a later pair with an equal key replaces an earlier one.
Type Parameters
Section titled “Type Parameters”K
V
Parameters
Section titled “Parameters”…[K, V][]
Returns
Section titled “Returns”HashMap<K, V>