Skip to content
Buy me a coffee

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.

K

V

new HashMap<K, V>(): HashMap<K, V>

Defined in: src/map/HashMap.ts:152

Creates an empty map.

HashMap<K, V>

AbstractMap.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.

number

number

HashMap<K, V>

IllegalArgumentException if initialCapacity is negative or NaN, or loadFactor is not positive

AbstractMap<K, V>.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.

Map<K, V>

HashMap<K, V>

NullPointerException if map is null or undefined

AbstractMap<K, V>.constructor

modCount: number = 0

Defined in: src/map/HashMap.ts:144

Structural modification count (new key, removal, clear); iterators compare against it.

[INSPECT](): string

Defined in: src/abstract/AbstractMap.ts:187

Node’s util.inspect hook: console.log prints the toString() form.

string

AbstractMap.[INSPECT]


[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().

IterableIterator<[K, V]>

AbstractMap.[iterator]


clear(): void

Defined in: src/map/HashMap.ts:529

Removes all mappings. Open iterators then fail with ConcurrentModificationException.

void

AbstractMap.clear


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.

HashMap<K, V>


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.

K

(key, value) => V | null | undefined

V | null

the new value, or null if there is now no mapping

NullPointerException if fn is null

ConcurrentModificationException if fn structurally modifies this map

AbstractMap.compute


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.

K

(key) => V | null | undefined

V | null

the existing non-null value, the newly stored value, or null

NullPointerException if fn is null

ConcurrentModificationException if fn structurally modifies this map

AbstractMap.computeIfAbsent


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.

K

(key, value) => V | null | undefined

V | null

the new value, or null if the key was absent or has been removed

NullPointerException if fn is null

ConcurrentModificationException if fn structurally modifies this map

AbstractMap.computeIfPresent


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.

unknown

boolean

AbstractMap.containsKey


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().

unknown

boolean

AbstractMap.containsValue


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).

Set<MapEntry<K, V>>

AbstractMap.entrySet


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”.

unknown

boolean

AbstractMap.equals


forEach(action): void

Defined in: src/map/HashMap.ts:615

Calls action for each mapping, in iteration order.

(key, value) => void

void

NullPointerException if action is null

ConcurrentModificationException if action structurally modifies this map

AbstractMap.forEach


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.

unknown

V | null

AbstractMap.get


getNode(key): HashNode<K, V> | undefined

Defined in: src/map/HashMap.ts:280

Returns the node for key, or undefined. Fires no hook.

unknown

HashNode<K, V> | undefined


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.

unknown

V

V

AbstractMap.getOrDefault

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.

unknown

V | null

V | null

AbstractMap.getOrDefault


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).

number

AbstractMap.hashCode


isEmpty(): boolean

Defined in: src/map/HashMap.ts:396

Returns true if this map has no mappings. O(1).

boolean

AbstractMap.isEmpty


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).

Set<K>

AbstractMap.keySet


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.

K

V

(oldValue, value) => V | null | undefined

V | null

the new value, or null if the mapping was removed

NullPointerException if value or fn is null

ConcurrentModificationException if fn structurally modifies this map

AbstractMap.merge


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.

K

V

V | null

the previous value, or null if there was no mapping

AbstractMap.put


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.

Map<K, V>

void

NullPointerException if m is null

AbstractMap.putAll


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.

K

V

V | null

the current value if it was non-null (and nothing changed), otherwise null

AbstractMap.putIfAbsent


remove(key): V | null

Defined in: src/map/HashMap.ts:496

Removes the mapping for key, if present. O(1) expected.

unknown

V | null

the removed value, or null if there was no mapping

AbstractMap.remove

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.

unknown

unknown

boolean

true if the mapping was removed

AbstractMap.remove


removeNode(node): void

Defined in: src/map/HashMap.ts:342

Removes exactly this node (by identity), then calls HashMap.afterNodeRemoval.

HashNode<K, V>

void


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.

K

V

V | null

the previous value, or null if there was no mapping

AbstractMap.replace

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.

K

V

V

boolean

true if the value was replaced

AbstractMap.replace


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.

(key, value) => V

void

NullPointerException if fn is null

ConcurrentModificationException if fn structurally modifies this map

AbstractMap.replaceAll


size(): number

Defined in: src/map/HashMap.ts:391

Returns the number of mappings. O(1).

number

AbstractMap.size


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).

string

AbstractMap.toString


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).

Collection<V>

AbstractMap.values


static newHashMap<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).

K

V

number

HashMap<K, V>

IllegalArgumentException if numMappings is negative or NaN


static of<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.

K

V

…[K, V][]

HashMap<K, V>