HashMap

class HashMap<K, V>(initialCapacity: Int = 16) : MutableMap<K, V> (source)

A mutable hash map associating each distinct key with one value.

Keys must retain stable equality and hash codes while stored. Nullable key and value types may store null; null keys hash to zero. Generic floating equality coalesces NaNs and distinguishes positive and negative zero. User equality and hashing callbacks retain their effects and exceptions.

Iteration order is unspecified. Read-only aliases and views observe later mutations. Inserting a new key, removing an existing key or clearing a non-empty map invalidates iterators; replacing a value does not. Storage, entries, views and iterators use the ordinary Guest heap and instruction budgets.

Parameters

initialCapacity

requested bucket capacity, not the initial size or an element limit.

Type Parameters

K

key type.

V

value type.

See also

Throws

if initialCapacity is negative or greater than 1,073,741,824.

Constructors

Link copied to clipboard
constructor(initialCapacity: Int = 16)

creates an empty map, rounding bucket capacity up to a power of two with a minimum of four; the default request is 16.

Properties

Link copied to clipboard
open override val entries: Set<Map.Entry<K, V>>

Live read-only set of this map's key-value entries in unspecified iteration order.

Link copied to clipboard
open override val keys: Set<K>

Live read-only set of this map's keys in unspecified iteration order.

Link copied to clipboard
open override var size: Int

Number of key-value associations currently stored in this map.

Link copied to clipboard
open override val values: Collection<V>

Live read-only collection of this map's values, including duplicates and nullable values.

Functions

Link copied to clipboard
open override fun clear()

Removes all associations while retaining bucket capacity for reuse.

Link copied to clipboard
operator fun <K, V> Map<K, V>.contains(key: K): Boolean

Checks key membership using key in map syntax.

Link copied to clipboard
open override fun containsKey(key: K): Boolean

Checks whether an association exists for key, even if its value is null.

Link copied to clipboard
open override fun containsValue(value: V): Boolean

Checks whether any stored value equals value.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean

Compares associations independently of iteration order, distinguishing missing keys from null values.

Link copied to clipboard
open operator override fun get(key: K): V?

Returns the value associated with key, or null when the key is absent.

Link copied to clipboard
inline fun <K, V> MutableMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V

Returns a non-null value for key, or computes and stores a default value.

Link copied to clipboard
open override fun hashCode(): Int

Sums entry hashes; each entry contributes key hash XOR value hash.

Link copied to clipboard
open override fun isEmpty(): Boolean

Checks whether this map contains no associations.

Link copied to clipboard
fun <K, V> Map<K, V>.isNotEmpty(): Boolean

Checks whether this map contains at least one association.

Link copied to clipboard
operator fun <K, V> Map<K, V>.iterator(): Iterator<Map.Entry<K, V>>

Returns an iterator over this map's entries, enabling for ((key, value) in map).

Link copied to clipboard
open override fun put(key: K, value: V): V?

Associates key with value, replacing the value for an equal existing key.

Link copied to clipboard
abstract fun putAll(p0: Map<out K?, out V?>?)
Link copied to clipboard
open override fun remove(key: K): V?

Removes the association for key, if one exists.

Link copied to clipboard
operator fun <K, V> MutableMap<K, V>.set(key: K, value: V)

Associates key with value using indexed assignment, discarding the previous value.

Link copied to clipboard
open override fun toString(): String

Formats associations as {key=value}; a direct self reference uses (this Map).