HashMap
A hash table implementation of MutableMap.
This class stores key-value pairs using a hash table data structure that provides fast lookups based on keys. It fully implements the MutableMap contract, providing all standard map operations including insertion, removal, and lookup of values by key.
Null keys and values
HashMap accepts null as a key. Since keys are unique, at most one entry with a null key can exist in the map. HashMap also accepts null as a value, and multiple entries can have null values.
Key's hash code and equality contracts
HashMap relies on the Any.hashCode and Any.equals functions of keys to organize and locate entries. Keys are considered equal if their Any.equals function returns true, and keys that are equal must have the same Any.hashCode value. Violating this contract can lead to incorrect behavior.
The Any.hashCode and Any.equals functions should be consistent and immutable during the lifetime of the key objects. Modifying a key object in a way that changes its hash code or equality after it has been used as a key in a HashMap may lead to the entry becoming unreachable.
Performance characteristics
The performance characteristics below assume that the Any.hashCode function of keys distributes them uniformly across the hash table, minimizing collisions. A poor hash function that causes many collisions can degrade performance.
HashMap provides efficient implementation for common operations:
Lookup (get, containsKey): O(1) time
Value search (containsValue): O(n) time, requires scanning all entries
Iteration order
HashMap does not guarantee any particular order for iteration over its keys, values, or entries. The iteration order is unpredictable and may change when the map is rehashed (when entries are added or removed and the internal capacity is adjusted). Do not rely on any specific iteration order.
If a predictable iteration order is required, consider using LinkedHashMap, which maintains insertion order.
Usage guidelines
HashMap uses an internal data structure with a finite capacity - the maximum number of entries it can store before needing to grow. As entries are added, the map tracks its load factor, which is the ratio of the number of entries to the current capacity. When this ratio exceeds a certain threshold, the map automatically increases its capacity and performs rehashing - rebuilding the internal data structure to redistribute entries. Rehashing is a relatively expensive operation that temporarily impacts performance. When creating a HashMap, you can optionally provide values for the initial capacity and load factor threshold. Note that these parameters are just hints for the implementation and can be ignored.
To optimize performance and memory usage:
If the number of entries is known in advance, use the constructor with initial capacity to avoid multiple rehashing operations as the map grows.
Choose an appropriate load factor when creating the map. A lower load factor reduces collision probability but uses more memory, while a higher load factor saves memory but may increase lookup time. The default load factor typically provides a good balance.
Ensure key objects have well-distributed Any.hashCode implementations to minimize collisions and maintain good performance.
Prefer putAll over multiple individual put calls when adding multiple entries.
Thread safety
HashMap is not thread-safe. If multiple threads access an instance concurrently and at least one thread modifies it, external synchronization is required.
Since Kotlin
1.0Parameters
the type of map keys. The map is invariant in its key type.
the type of map values. The mutable map is invariant in its value type.
A hash table implementation of MutableMap.
This class stores key-value pairs using a hash table data structure that provides fast lookups based on keys. It fully implements the MutableMap contract, providing all standard map operations including insertion, removal, and lookup of values by key.
Null keys and values
HashMap accepts null as a key. Since keys are unique, at most one entry with a null key can exist in the map. HashMap also accepts null as a value, and multiple entries can have null values.
Key's hash code and equality contracts
HashMap relies on the Any.hashCode and Any.equals functions of keys to organize and locate entries. Keys are considered equal if their Any.equals function returns true, and keys that are equal must have the same Any.hashCode value. Violating this contract can lead to incorrect behavior.
The Any.hashCode and Any.equals functions should be consistent and immutable during the lifetime of the key objects. Modifying a key object in a way that changes its hash code or equality after it has been used as a key in a HashMap may lead to the entry becoming unreachable.
Performance characteristics
The performance characteristics below assume that the Any.hashCode function of keys distributes them uniformly across the hash table, minimizing collisions. A poor hash function that causes many collisions can degrade performance.
HashMap provides efficient implementation for common operations:
Lookup (get, containsKey): O(1) time
Value search (containsValue): O(n) time, requires scanning all entries
Iteration order
HashMap does not guarantee any particular order for iteration over its keys, values, or entries. The iteration order is unpredictable and may change when the map is rehashed (when entries are added or removed and the internal capacity is adjusted). Do not rely on any specific iteration order.
If a predictable iteration order is required, consider using LinkedHashMap, which maintains insertion order.
Usage guidelines
HashMap uses an internal data structure with a finite capacity - the maximum number of entries it can store before needing to grow. When the map becomes full, it automatically increases its capacity and performs rehashing - rebuilding the internal data structure to redistribute entries. Rehashing is a relatively expensive operation that temporarily impacts performance. When creating a HashMap, you can optionally provide an initial capacity value, which will be used to size the internal data structure, potentially avoiding rehashing operations as the map grows.
To optimize performance and memory usage:
If the number of entries is known in advance, use the constructor with initial capacity to avoid multiple rehashing operations as the map grows.
Ensure key objects have well-distributed Any.hashCode implementations to minimize collisions and maintain good performance.
Prefer putAll over multiple individual put calls when adding multiple entries.
Thread safety
HashMap is not thread-safe. If multiple threads access an instance concurrently and at least one thread modifies it, external synchronization is required.
Since Kotlin
1.1Parameters
the type of map keys. The map is invariant in its key type.
the type of map values. The mutable map is invariant in its value type.
Inheritors
A hash table implementation of MutableMap.
This class stores key-value pairs using a hash table data structure that provides fast lookups based on keys. It fully implements the MutableMap contract, providing all standard map operations including insertion, removal, and lookup of values by key.
Null keys and values
HashMap accepts null as a key. Since keys are unique, at most one entry with a null key can exist in the map. HashMap also accepts null as a value, and multiple entries can have null values.
Key's hash code and equality contracts
HashMap relies on the Any.hashCode and Any.equals functions of keys to organize and locate entries. Keys are considered equal if their Any.equals function returns true, and keys that are equal must have the same Any.hashCode value. Violating this contract can lead to incorrect behavior.
The Any.hashCode and Any.equals functions should be consistent and immutable during the lifetime of the key objects. Modifying a key object in a way that changes its hash code or equality after it has been used as a key in a HashMap may lead to the entry becoming unreachable.
Performance characteristics
The performance characteristics below assume that the Any.hashCode function of keys distributes them uniformly across the hash table, minimizing collisions. A poor hash function that causes many collisions can degrade performance.
HashMap provides efficient implementation for common operations:
Lookup (get, containsKey): O(1) time
Insertion and removal (put, remove): O(1) time
Value search (containsValue): O(n) time, requires scanning all entries
Iteration (entries, keys, values): O(n) time
Iteration order
HashMap does not guarantee any particular order for iteration over its keys, values, or entries. The iteration order is unpredictable and may change when the map is rehashed (when entries are added or removed and the internal capacity is adjusted). Do not rely on any specific iteration order.
If a predictable iteration order is required, consider using LinkedHashMap, which maintains insertion order.
Usage guidelines
HashMap uses an internal data structure with a finite capacity - the maximum number of entries it can store before needing to grow. As entries are added, the map tracks its load factor, which is the ratio of the number of entries to the current capacity. When this ratio exceeds a certain threshold, the map automatically increases its capacity and performs rehashing - rebuilding the internal data structure to redistribute entries. Rehashing is a relatively expensive operation that temporarily impacts performance. When creating a HashMap, you can optionally provide values for the initial capacity and load factor threshold.
To optimize performance and memory usage:
If the number of entries is known in advance, use the constructor with initial capacity to avoid multiple rehashing operations as the map grows.
Choose an appropriate load factor when creating the map. A lower load factor reduces collision probability but uses more memory, while a higher load factor saves memory but may increase lookup time. The default load factor typically provides a good balance.
Ensure key objects have well-distributed Any.hashCode implementations to minimize collisions and maintain good performance.
Prefer putAll over multiple individual put calls when adding multiple entries.
Thread safety
HashMap is not thread-safe. If multiple threads access an instance concurrently and at least one thread modifies it, external synchronization is required.
Since Kotlin
1.1Parameters
the type of map keys. The map is invariant in its key type.
the type of map values. The mutable map is invariant in its value type.
A hash table implementation of MutableMap.
This class stores key-value pairs using a hash table data structure that provides fast lookups based on keys. It fully implements the MutableMap contract, providing all standard map operations including insertion, removal, and lookup of values by key.
Null keys and values
HashMap accepts null as a key. Since keys are unique, at most one entry with a null key can exist in the map. HashMap also accepts null as a value, and multiple entries can have null values.
Key's hash code and equality contracts
HashMap relies on the Any.hashCode and Any.equals functions of keys to organize and locate entries. Keys are considered equal if their Any.equals function returns true, and keys that are equal must have the same Any.hashCode value. Violating this contract can lead to incorrect behavior.
The Any.hashCode and Any.equals functions should be consistent and immutable during the lifetime of the key objects. Modifying a key object in a way that changes its hash code or equality after it has been used as a key in a HashMap may lead to the entry becoming unreachable.
Performance characteristics
The performance characteristics below assume that the Any.hashCode function of keys distributes them uniformly across the hash table, minimizing collisions. A poor hash function that causes many collisions can degrade performance.
HashMap provides efficient implementation for common operations:
Lookup (get, containsKey): O(1) time
Value search (containsValue): O(n) time, requires scanning all entries
Iteration order
HashMap does not guarantee any particular order for iteration over its keys, values, or entries. The iteration order is unpredictable and may change when the map is rehashed (when entries are added or removed and the internal capacity is adjusted). Do not rely on any specific iteration order.
If a predictable iteration order is required, consider using LinkedHashMap, which maintains insertion order.
Usage guidelines
HashMap uses an internal data structure with a finite capacity - the maximum number of entries it can store before needing to grow. When the map becomes full, it automatically increases its capacity and performs rehashing - rebuilding the internal data structure to redistribute entries. Rehashing is a relatively expensive operation that temporarily impacts performance. When creating a HashMap, you can optionally provide an initial capacity value, which will be used to size the internal data structure, potentially avoiding rehashing operations as the map grows.
To optimize performance and memory usage:
If the number of entries is known in advance, use the constructor with initial capacity to avoid multiple rehashing operations as the map grows.
Ensure key objects have well-distributed Any.hashCode implementations to minimize collisions and maintain good performance.
Prefer putAll over multiple individual put calls when adding multiple entries.
Thread safety
HashMap is not thread-safe. If multiple threads access an instance concurrently and at least one thread modifies it, external synchronization is required.
Since Kotlin
1.3Parameters
the type of map keys. The map is invariant in its key type.
the type of map values. The mutable map is invariant in its value type.
A hash table implementation of MutableMap.
This class stores key-value pairs using a hash table data structure that provides fast lookups based on keys. It fully implements the MutableMap contract, providing all standard map operations including insertion, removal, and lookup of values by key.
Null keys and values
HashMap accepts null as a key. Since keys are unique, at most one entry with a null key can exist in the map. HashMap also accepts null as a value, and multiple entries can have null values.
Key's hash code and equality contracts
HashMap relies on the Any.hashCode and Any.equals functions of keys to organize and locate entries. Keys are considered equal if their Any.equals function returns true, and keys that are equal must have the same Any.hashCode value. Violating this contract can lead to incorrect behavior.
The Any.hashCode and Any.equals functions should be consistent and immutable during the lifetime of the key objects. Modifying a key object in a way that changes its hash code or equality after it has been used as a key in a HashMap may lead to the entry becoming unreachable.
Performance characteristics
The performance characteristics below assume that the Any.hashCode function of keys distributes them uniformly across the hash table, minimizing collisions. A poor hash function that causes many collisions can degrade performance.
HashMap provides efficient implementation for common operations:
Lookup (get, containsKey): O(1) time
Value search (containsValue): O(n) time, requires scanning all entries
Iteration order
HashMap does not guarantee any particular order for iteration over its keys, values, or entries. The iteration order is unpredictable and may change when the map is rehashed (when entries are added or removed and the internal capacity is adjusted). Do not rely on any specific iteration order.
If a predictable iteration order is required, consider using LinkedHashMap, which maintains insertion order.
Usage guidelines
HashMap uses an internal data structure with a finite capacity - the maximum number of entries it can store before needing to grow. When the map becomes full, it automatically increases its capacity and performs rehashing - rebuilding the internal data structure to redistribute entries. Rehashing is a relatively expensive operation that temporarily impacts performance. When creating a HashMap, you can optionally provide an initial capacity value, which will be used to size the internal data structure, potentially avoiding rehashing operations as the map grows.
To optimize performance and memory usage:
If the number of entries is known in advance, use the constructor with initial capacity to avoid multiple rehashing operations as the map grows.
Ensure key objects have well-distributed Any.hashCode implementations to minimize collisions and maintain good performance.
Prefer putAll over multiple individual put calls when adding multiple entries.
Thread safety
HashMap is not thread-safe. If multiple threads access an instance concurrently and at least one thread modifies it, external synchronization is required.
Since Kotlin
1.8Parameters
the type of map keys. The map is invariant in its key type.
the type of map values. The mutable map is invariant in its value type.
A hash table implementation of MutableMap.
This class stores key-value pairs using a hash table data structure that provides fast lookups based on keys. It fully implements the MutableMap contract, providing all standard map operations including insertion, removal, and lookup of values by key.
Null keys and values
HashMap accepts null as a key. Since keys are unique, at most one entry with a null key can exist in the map. HashMap also accepts null as a value, and multiple entries can have null values.
Key's hash code and equality contracts
HashMap relies on the Any.hashCode and Any.equals functions of keys to organize and locate entries. Keys are considered equal if their Any.equals function returns true, and keys that are equal must have the same Any.hashCode value. Violating this contract can lead to incorrect behavior.
The Any.hashCode and Any.equals functions should be consistent and immutable during the lifetime of the key objects. Modifying a key object in a way that changes its hash code or equality after it has been used as a key in a HashMap may lead to the entry becoming unreachable.
Performance characteristics
The performance characteristics below assume that the Any.hashCode function of keys distributes them uniformly across the hash table, minimizing collisions. A poor hash function that causes many collisions can degrade performance.
HashMap provides efficient implementation for common operations:
Lookup (get, containsKey): O(1) time
Value search (containsValue): O(n) time, requires scanning all entries
Iteration order
HashMap does not guarantee any particular order for iteration over its keys, values, or entries. The iteration order is unpredictable and may change when the map is rehashed (when entries are added or removed and the internal capacity is adjusted). Do not rely on any specific iteration order.
If a predictable iteration order is required, consider using LinkedHashMap, which maintains insertion order.
Usage guidelines
HashMap uses an internal data structure with a finite capacity - the maximum number of entries it can store before needing to grow. When the map becomes full, it automatically increases its capacity and performs rehashing - rebuilding the internal data structure to redistribute entries. Rehashing is a relatively expensive operation that temporarily impacts performance. When creating a HashMap, you can optionally provide an initial capacity value, which will be used to size the internal data structure, potentially avoiding rehashing operations as the map grows.
To optimize performance and memory usage:
If the number of entries is known in advance, use the constructor with initial capacity to avoid multiple rehashing operations as the map grows.
Ensure key objects have well-distributed Any.hashCode implementations to minimize collisions and maintain good performance.
Prefer putAll over multiple individual put calls when adding multiple entries.
Thread safety
HashMap is not thread-safe. If multiple threads access an instance concurrently and at least one thread modifies it, external synchronization is required.
Since Kotlin
1.8Parameters
the type of map keys. The map is invariant in its key type.
the type of map values. The mutable map is invariant in its value type.
Constructors
Creates a new empty HashMap.
Creates a new empty HashMap with the specified initial capacity.
Creates a new empty HashMap with the specified initial capacity and load factor.
Creates a new empty HashMap.
Creates a new empty HashMap with the specified initial capacity.
Creates a new empty HashMap with the specified initial capacity and load factor.
Creates a new empty HashMap.
Creates a new empty HashMap with the specified initial capacity.
Creates a new empty HashMap with the specified initial capacity and load factor.
Creates a new empty HashMap.
Creates a new empty HashMap with the specified initial capacity.
Creates a new empty HashMap with the specified initial capacity and load factor.
Properties
Returns a MutableSet of all key/value pairs in this map.
Returns a MutableSet of all key/value pairs in this map.
Returns a MutableSet of all key/value pairs in this map.
Returns a MutableSet of all key/value pairs in this map.
Returns a MutableSet of all key/value pairs in this map.
Returns a MutableSet of all keys in this map.
Returns a read-only Set of all keys in this map.
Returns a MutableSet of all keys in this map.
Returns a MutableSet of all keys in this map.
Returns a MutableSet of all keys in this map.
Returns a MutableCollection of all values in this map. Note that this collection may contain duplicate values.
Returns a read-only Collection of all values in this map.
Returns a MutableCollection of all values in this map. Note that this collection may contain duplicate values.
Returns a MutableCollection of all values in this map. Note that this collection may contain duplicate values.
Returns a MutableCollection of all values in this map. Note that this collection may contain duplicate values.
Functions
Returns a view with the JsMap methods to consume it in JavaScript as a regular Map. Structural changes in the base map are synchronized with the view, and vice versa.
Returns a view with the JsReadonlyMap methods to consume it in JavaScript as a regular readonly Map. Structural changes in the base map are synchronized with the view.
Returns true if the map contains the specified key.
Returns true if the map contains the specified key.
Returns true if the map contains the specified key.
Returns true if the map contains the specified key.
Returns true if the map contains the specified key.
Returns true if the map maps one or more keys to the specified value.
Returns true if the map maps one or more keys to the specified value.
Returns true if the map maps one or more keys to the specified value.
Returns true if the map maps one or more keys to the specified value.
Returns true if the map maps one or more keys to the specified value.
Compares this map with other instance with the ordered structural equality.
Indicates whether some other object is "equal to" this one.
Indicates whether some other object is "equal to" this one.
Returns a single list of all elements yielded from results of transform function being invoked on each entry of original map.
Returns a single list of all elements yielded from results of transform function being invoked on each entry of original map.
Appends all elements yielded from results of transform function being invoked on each entry of original map, to the given destination.
Appends all elements yielded from results of transform function being invoked on each entry of original map, to the given destination.
Returns the value corresponding to the given key, or null if such a key is not present in the map.
Returns the value corresponding to the given key, or null if such a key is not present in the map.
Returns the value corresponding to the given key, or null if such a key is not present in the map.
Returns the value corresponding to the given key, or null if such a key is not present in the map.
Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
Returns the value for the given key or throws an exception if there is no such key in the map.
Returns the value of the property for the given object from this read-only map.
Returns the value of the property for the given object from this mutable map.
Returns true if the map is empty (contains no elements), false otherwise.
Returns true if the map is empty (contains no elements), false otherwise.
Returns true if the map is empty (contains no elements), false otherwise.
Returns true if the map is empty (contains no elements), false otherwise.
Returns a MutableIterator over the mutable entries in the MutableMap.
Populates the given destination map with entries having the keys obtained by applying the transform function to each entry in this Map and the values of this map.
Populates the given destination map with entries having the keys of this map and the values obtained by applying the transform function to each entry in this Map.
Returns the largest value among all values produced by selector function applied to each entry in the map.
Returns the largest value according to the provided comparator among all values produced by selector function applied to each entry in the map or null if the map is empty.
Returns the first entry having the largest value according to the provided comparator or null if there are no entries.
Returns the smallest value among all values produced by selector function applied to each entry in the map.
Returns the smallest value according to the provided comparator among all values produced by selector function applied to each entry in the map or null if the map is empty.
Returns a map containing all entries of the original map except the entry with the given key.
Returns a map containing all entries of the original map except those entries the keys of which are contained in the given keys array.
Returns a map containing all entries of the original map except those entries the keys of which are contained in the given keys collection.
Returns a map containing all entries of the original map except those entries the keys of which are contained in the given keys sequence.
Removes the entry with the given key from this mutable map.
Removes all entries the keys of which are contained in the given keys array from this mutable map.
Removes all entries the keys of which are contained in the given keys collection from this mutable map.
Removes all entries from the keys of which are contained in the given keys sequence from this mutable map.
Returns the first entry having the smallest value according to the provided comparator or null if there are no entries.
Creates a new read-only map by replacing or adding entries to this map from a given array of key-value pairs.
Creates a new read-only map by replacing or adding an entry to this map from a given key-value pair.
Creates a new read-only map by replacing or adding entries to this map from a given collection of key-value pairs.
Creates a new read-only map by replacing or adding entries to this map from another map.
Creates a new read-only map by replacing or adding entries to this map from a given sequence of key-value pairs.
Appends or replaces all pairs from the given array of pairs in this mutable map.
Appends or replaces the given pair in this mutable map.
Appends or replaces all pairs from the given collection of pairs in this mutable map.
Appends or replaces all entries from the given map in this mutable map.
Appends or replaces all pairs from the given sequence of pairs in this mutable map.
Updates this map with key/value pairs from the specified map from.
Updates this map with key/value pairs from the specified map from.
Updates this map with key/value pairs from the specified map from.
Updates this map with key/value pairs from the specified map from.
Puts all the given pairs into this MutableMap with the first component in the pair being the key and the second the value.
Puts all the elements of the given collection into this MutableMap with the first component in the pair being the key and the second the value.
Puts all the elements of the given sequence into this MutableMap with the first component in the pair being the key and the second the value.
Removes the specified key and its corresponding value from this map.
Removes the specified key and its corresponding value from this map.
Removes the specified key and its corresponding value from this map.
Removes the specified key and its corresponding value from this map.
Removes the specified key and its corresponding value from this map.
Removes the specified key and its corresponding value from this map.
Removes the entry for the specified key only if it is currently mapped to the specified value.
Returns a new read-only map containing all key-value pairs from the original map.
Populates and returns the destination mutable map with key-value pairs from the given map.
Returns a new mutable map containing all key-value pairs from the original map.
Converts this Map to a Properties object.
Converts this Map to a SortedMap. The resulting SortedMap determines the equality and order of keys according to their natural sorting order.
Converts this Map to a SortedMap. The resulting SortedMap determines the equality and order of keys according to the sorting order provided by the given comparator.
Returns a wrapper of this read-only map, having the implicit default value provided with the specified function defaultValue.
Returns a wrapper of this mutable map, having the implicit default value provided with the specified function defaultValue.