Skip to content
Buy me a coffee

LinkedHashMap

Defined in: src/map/LinkedHashMap.ts:59

A HashMap whose entries are also linked in a doubly linked list, giving a predictable iteration order and the SequencedMap operations. The order is insertion order by default (putting an existing key again keeps its place), or access order when constructed with accessOrder = true (every get, getOrDefault, put, putIfAbsent, replace, compute* or merge hit moves the key to the end, which is a structural change for open iterators). null and undefined keys and values are allowed. Lookups and updates are O(1) expected; first/last access and polling are O(1).

Override LinkedHashMap.removeEldestEntry, or use LinkedHashMap.lru, to build an LRU cache.

K

V

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

Defined in: src/map/LinkedHashMap.ts:76

Creates an empty insertion-ordered map.

LinkedHashMap<K, V>

HashMap.constructor

new LinkedHashMap<K, V>(initialCapacity, loadFactor?, accessOrder?): LinkedHashMap<K, V>

Defined in: src/map/LinkedHashMap.ts:82

Creates an empty map. initialCapacity and loadFactor are validated sizing hints only; accessOrder = true selects access order instead of insertion order.

number

number

boolean

LinkedHashMap<K, V>

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

HashMap.constructor

new LinkedHashMap<K, V>(map): LinkedHashMap<K, V>

Defined in: src/map/LinkedHashMap.ts:87

Creates an insertion-ordered map with the mappings of map, in map’s iteration order.

Map<K, V>

LinkedHashMap<K, V>

NullPointerException if map is null or undefined

HashMap.constructor

readonly accessOrder: boolean

Defined in: src/map/LinkedHashMap.ts:65

true if the map iterates in access order (least recently used first), false for insertion order.


modCount: number = 0

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

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

HashMap.modCount

[INSPECT](): string

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

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

string

HashMap.[INSPECT]


[iterator](): IterableIterator<[K, V]>

Defined in: src/map/LinkedHashMap.ts:323

Returns a fail-fast iterator over the mappings as [key, value] pairs, in insertion or access order.

IterableIterator<[K, V]>

SequencedMap.[iterator]

HashMap.[iterator]


clear(): void

Defined in: src/map/LinkedHashMap.ts:273

Removes all mappings.

void

SequencedMap.clear

HashMap.clear


clone(): LinkedHashMap<K, V>

Defined in: src/map/LinkedHashMap.ts:125

Shallow copy of the same runtime class, in the same order and mode; the copy evicts nothing while copying.

LinkedHashMap<K, V>

HashMap.clone


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

SequencedMap.compute

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

SequencedMap.computeIfAbsent

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

SequencedMap.computeIfPresent

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

SequencedMap.containsKey

HashMap.containsKey


containsValue(value): boolean

Defined in: src/map/LinkedHashMap.ts:267

Returns true if one or more keys map to a value equal to value. O(n), walking the list.

unknown

boolean

SequencedMap.containsValue

HashMap.containsValue


entrySet(): SequencedSet<MapEntry<K, V>>

Defined in: src/map/LinkedHashMap.ts:318

Returns a live view of the mappings in this map’s order; same as sequencedEntrySet(). Its entries are the map’s own nodes, so setValue writes through; it does not support add (UnsupportedOperationException).

SequencedSet<MapEntry<K, V>>

SequencedMap.entrySet

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

SequencedMap.equals

HashMap.equals


firstEntry(): MapEntry<K, V> | null

Defined in: src/map/LinkedHashMap.ts:366

Returns the first (eldest) entry as an immutable snapshot, or null if this map is empty. O(1); it does not count as an access.

MapEntry<K, V> | null

SequencedMap.firstEntry


forEach(action): void

Defined in: src/map/LinkedHashMap.ts:289

Calls action for each mapping, in insertion or access order.

(key, value) => void

void

NullPointerException if action is null

ConcurrentModificationException if action structurally modifies this map

SequencedMap.forEach

HashMap.forEach


get(key): V | null

Defined in: src/map/LinkedHashMap.ts:252

Returns the value mapped to key, or null if there is none (or the key maps to null). O(1) expected. In access order a hit moves the key to the end, a structural change for open iterators.

unknown

V | null

SequencedMap.get

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

HashMap.getNode


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

SequencedMap.getOrDefault

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

SequencedMap.getOrDefault

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

SequencedMap.hashCode

HashMap.hashCode


isEmpty(): boolean

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

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

boolean

SequencedMap.isEmpty

HashMap.isEmpty


keySet(): SequencedSet<K>

Defined in: src/map/LinkedHashMap.ts:302

Returns a live view of the keys in this map’s order; same as sequencedKeySet(). Removing from it removes the mapping; it does not support add (UnsupportedOperationException).

SequencedSet<K>

SequencedMap.keySet

HashMap.keySet


lastEntry(): MapEntry<K, V> | null

Defined in: src/map/LinkedHashMap.ts:375

Returns the last (youngest) entry as an immutable snapshot, or null if this map is empty. O(1); it does not count as an access.

MapEntry<K, V> | null

SequencedMap.lastEntry


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

SequencedMap.merge

HashMap.merge


pollFirstEntry(): MapEntry<K, V> | null

Defined in: src/map/LinkedHashMap.ts:381

Removes and returns the first (eldest) entry as an immutable snapshot, or null if this map is empty. O(1).

MapEntry<K, V> | null

SequencedMap.pollFirstEntry


pollLastEntry(): MapEntry<K, V> | null

Defined in: src/map/LinkedHashMap.ts:389

Removes and returns the last (youngest) entry as an immutable snapshot, or null if this map is empty. O(1).

MapEntry<K, V> | null

SequencedMap.pollLastEntry


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

SequencedMap.put

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

SequencedMap.putAll

HashMap.putAll


putFirst(key, value): V | null

Defined in: src/map/LinkedHashMap.ts:401

Maps key to value and moves the mapping to the front, in either order mode. A new key may be evicted at once by LinkedHashMap.removeEldestEntry.

K

V

V | null

the previous value, or null if there was no mapping

SequencedMap.putFirst


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

SequencedMap.putIfAbsent

HashMap.putIfAbsent


putLast(key, value): V | null

Defined in: src/map/LinkedHashMap.ts:409

Maps key to value and moves the mapping to the end, in either order mode.

K

V

V | null

the previous value, or null if there was no mapping

SequencedMap.putLast


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

SequencedMap.remove

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

SequencedMap.remove

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

HashMap.removeNode


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

SequencedMap.replace

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

SequencedMap.replace

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

SequencedMap.replaceAll

HashMap.replaceAll


reversed(): SequencedMap<K, V>

Defined in: src/map/LinkedHashMap.ts:358

Returns a live reverse-ordered view: changes through either side are visible in the other. Writes go to this map: a new key put through the view still lands at this map’s end, and putFirst/putLast on the view put at this map’s end/front.

SequencedMap<K, V>

SequencedMap.reversed


sequencedEntrySet(): SequencedSet<MapEntry<K, V>>

Defined in: src/map/LinkedHashMap.ts:349

Returns a live, sequenced view of the mappings in this map’s order. Its entries are the map’s own nodes, so setValue writes through; adding throws UnsupportedOperationException.

SequencedSet<MapEntry<K, V>>

SequencedMap.sequencedEntrySet


sequencedKeySet(): SequencedSet<K>

Defined in: src/map/LinkedHashMap.ts:333

Returns a live, sequenced view of the keys in this map’s order. Removal (including removeFirst/removeLast) writes through; add, addFirst and addLast throw UnsupportedOperationException.

SequencedSet<K>

SequencedMap.sequencedKeySet


sequencedValues(): SequencedCollection<V>

Defined in: src/map/LinkedHashMap.ts:341

Returns a live, sequenced view of the values in this map’s order. Removal writes through; adding throws UnsupportedOperationException.

SequencedCollection<V>

SequencedMap.sequencedValues


size(): number

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

Returns the number of mappings. O(1).

number

SequencedMap.size

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

SequencedMap.toString

HashMap.toString


values(): SequencedCollection<V>

Defined in: src/map/LinkedHashMap.ts:310

Returns a live view of the values in this map’s order; same as sequencedValues(). Removing from it removes the mapping; it does not support add (UnsupportedOperationException).

SequencedCollection<V>

SequencedMap.values

HashMap.values


static lru<K, V>(maxSize): LinkedHashMap<K, V>

Defined in: src/map/LinkedHashMap.ts:108

Returns an empty access-ordered map that evicts its least recently used entry whenever an insertion takes it above maxSize entries: a ready-made LRU cache.

K

V

number

LinkedHashMap<K, V>

IllegalArgumentException if maxSize is less than 1 or NaN


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

HashMap.newHashMap


static newLinkedHashMap<K, V>(numMappings): LinkedHashMap<K, V>

Defined in: src/map/LinkedHashMap.ts:119

Returns an empty insertion-ordered map intended for numMappings mappings (a sizing hint only).

K

V

number

LinkedHashMap<K, V>

IllegalArgumentException if numMappings is negative or NaN


static of<K, V>(…pairs): LinkedHashMap<K, V>

Defined in: src/map/LinkedHashMap.ts:97

Returns a new insertion-ordered map of the given [key, value] pairs; a repeated key keeps its first position.

K

V

…[K, V][]

LinkedHashMap<K, V>

HashMap.of