Core API – Kotlin Programming Language (original) (raw)

Map

A collection that holds pairs of objects (keys and values) and supports retrieving the value corresponding to each key, checking if a collection holds a particular key or a value. Maps also allow iterating over keys, values or key-value pairs (entries). Complex operations are built upon this functionality and provided in form of kotlin.collections extension functions.

Map keys are unique; the map holds only one value for each key. In contrast, the same value can be associated with several unique keys.

It is implementation-specific how Map defines key's uniqueness. If not stated otherwise, Map implementations are usually distinguishing elements using Any.equals. However, it is not the only way to distinguish elements, and some implementations may use referential equality or compare elements by some of their properties. It is recommended to explicitly specify how a class implementing Map distinguish elements.

It is also implementation-specific how Map handles null keys and values: some Map implementations may support them, while other may not. It is recommended to explicitly define key/value nullability policy when implementing Map.

Unlike Collection implementations, Map implementations must override Any.toString, Any.equals and Any.hashCode functions and provide implementations such that:

var hashCode: Int = 0
for ((k, v) in entries) hashCode += k.hashCode() ^ v.hashCode()

Functions in this interface support only read-only access to the map; read-write access is supported through the MutableMap interface.

Since Kotlin

1.0

K

the type of map keys. The map is invariant in its key type, as it can accept a key as a parameter (of containsKey for example) and return it in a keys set.

V

the type of map values. The map is covariant in its value type.

A collection that holds pairs of objects (keys and values) and supports retrieving the value corresponding to each key, checking if a collection holds a particular key or a value. Maps also allow iterating over keys, values or key-value pairs (entries). Complex operations are built upon this functionality and provided in form of kotlin.collections extension functions.

Map keys are unique; the map holds only one value for each key. In contrast, the same value can be associated with several unique keys.

It is implementation-specific how Map defines key's uniqueness. If not stated otherwise, Map implementations are usually distinguishing elements using Any.equals. However, it is not the only way to distinguish elements, and some implementations may use referential equality or compare elements by some of their properties. It is recommended to explicitly specify how a class implementing Map distinguish elements.

It is also implementation-specific how Map handles null keys and values: some Map implementations may support them, while other may not. It is recommended to explicitly define key/value nullability policy when implementing Map.

Unlike Collection implementations, Map implementations must override Any.toString, Any.equals and Any.hashCode functions and provide implementations such that:

var hashCode: Int = 0
for ((k, v) in entries) hashCode += k.hashCode() ^ v.hashCode()

Functions in this interface support only read-only access to the map; read-write access is supported through the MutableMap interface.

Since Kotlin

1.1

K

the type of map keys. The map is invariant in its key type, as it can accept a key as a parameter (of containsKey for example) and return it in a keys set.

V

the type of map values. The map is covariant in its value type.

A collection that holds pairs of objects (keys and values) and supports retrieving the value corresponding to each key, checking if a collection holds a particular key or a value. Maps also allow iterating over keys, values or key-value pairs (entries). Complex operations are built upon this functionality and provided in form of kotlin.collections extension functions.

Map keys are unique; the map holds only one value for each key. In contrast, values may be duplicated; there might be several unique keys associated with the same value.

It is implementation-specific how Map defines key's uniqueness. If not stated otherwise, Map implementations are usually distinguishing elements using Any.equals. However, it is not the only way to distinguish elements, and some implementations may use referential equality or compare elements by some of their properties. It is recommended to explicitly specify how a class implementing Map distinguish elements.

It is also implementation-specific how Map handles null keys and values: some Map implementations may support them, while other may not. It is recommended to explicitly define key/value nullability policy when implementing Map.

Unlike Collection implementations, Map implementations must override Any.toString, Any.equals and Any.hashCode functions and provide implementations such that:

var hashCode: Int = 0
for ((k, v) in entries) hashCode += (k?.hashCode() ?: 0) ^ (v?.hashCode() ?: 0)

Functions in this interface support only read-only access to the map; read-write access is supported through the MutableMap interface.

By convention, Map implementations are usually also implement java.io.Serializable.

Since Kotlin

1.0

K

the type of map keys. The map is invariant in its key type, as it can accept a key as a parameter (of containsKey for example) and return it in a keys set.

V

the type of map values. The map is covariant in its value type.

A collection that holds pairs of objects (keys and values) and supports retrieving the value corresponding to each key, checking if a collection holds a particular key or a value. Maps also allow iterating over keys, values or key-value pairs (entries). Complex operations are built upon this functionality and provided in form of kotlin.collections extension functions.

Map keys are unique; the map holds only one value for each key. In contrast, the same value can be associated with several unique keys.

It is implementation-specific how Map defines key's uniqueness. If not stated otherwise, Map implementations are usually distinguishing elements using Any.equals. However, it is not the only way to distinguish elements, and some implementations may use referential equality or compare elements by some of their properties. It is recommended to explicitly specify how a class implementing Map distinguish elements.

It is also implementation-specific how Map handles null keys and values: some Map implementations may support them, while other may not. It is recommended to explicitly define key/value nullability policy when implementing Map.

Unlike Collection implementations, Map implementations must override Any.toString, Any.equals and Any.hashCode functions and provide implementations such that:

var hashCode: Int = 0
for ((k, v) in entries) hashCode += k.hashCode() ^ v.hashCode()

Functions in this interface support only read-only access to the map; read-write access is supported through the MutableMap interface.

Since Kotlin

1.3

K

the type of map keys. The map is invariant in its key type, as it can accept a key as a parameter (of containsKey for example) and return it in a keys set.

V

the type of map values. The map is covariant in its value type.

A collection that holds pairs of objects (keys and values) and supports retrieving the value corresponding to each key, checking if a collection holds a particular key or a value. Maps also allow iterating over keys, values or key-value pairs (entries). Complex operations are built upon this functionality and provided in form of kotlin.collections extension functions.

Map keys are unique; the map holds only one value for each key. In contrast, the same value can be associated with several unique keys.

It is implementation-specific how Map defines key's uniqueness. If not stated otherwise, Map implementations are usually distinguishing elements using Any.equals. However, it is not the only way to distinguish elements, and some implementations may use referential equality or compare elements by some of their properties. It is recommended to explicitly specify how a class implementing Map distinguish elements.

It is also implementation-specific how Map handles null keys and values: some Map implementations may support them, while other may not. It is recommended to explicitly define key/value nullability policy when implementing Map.

Unlike Collection implementations, Map implementations must override Any.toString, Any.equals and Any.hashCode functions and provide implementations such that:

var hashCode: Int = 0
for ((k, v) in entries) hashCode += k.hashCode() ^ v.hashCode()

Functions in this interface support only read-only access to the map; read-write access is supported through the MutableMap interface.

Since Kotlin

1.8

K

the type of map keys. The map is invariant in its key type, as it can accept a key as a parameter (of containsKey for example) and return it in a keys set.

V

the type of map values. The map is covariant in its value type.

A collection that holds pairs of objects (keys and values) and supports retrieving the value corresponding to each key, checking if a collection holds a particular key or a value. Maps also allow iterating over keys, values or key-value pairs (entries). Complex operations are built upon this functionality and provided in form of kotlin.collections extension functions.

Map keys are unique; the map holds only one value for each key. In contrast, the same value can be associated with several unique keys.

It is implementation-specific how Map defines key's uniqueness. If not stated otherwise, Map implementations are usually distinguishing elements using Any.equals. However, it is not the only way to distinguish elements, and some implementations may use referential equality or compare elements by some of their properties. It is recommended to explicitly specify how a class implementing Map distinguish elements.

It is also implementation-specific how Map handles null keys and values: some Map implementations may support them, while other may not. It is recommended to explicitly define key/value nullability policy when implementing Map.

Unlike Collection implementations, Map implementations must override Any.toString, Any.equals and Any.hashCode functions and provide implementations such that:

var hashCode: Int = 0
for ((k, v) in entries) hashCode += k.hashCode() ^ v.hashCode()

Functions in this interface support only read-only access to the map; read-write access is supported through the MutableMap interface.

Since Kotlin

1.8

K

the type of map keys. The map is invariant in its key type, as it can accept a key as a parameter (of containsKey for example) and return it in a keys set.

V

the type of map values. The map is covariant in its value type.

expect interface Entry<out K, out V>

Represents a key/value pair held by a Map.

actual interface Entry<out K, out V>

Represents a key/value pair held by a Map.

actual interface Entry<out K, out V>

Represents a key/value pair held by a Map.

actual interface Entry<out K, out V>

Represents a key/value pair held by a Map.

actual interface Entry<out K, out V>

Represents a key/value pair held by a Map.

actual interface Entry<out K, out V>

Represents a key/value pair held by a Map.

Returns a read-only Set of all key/value pairs in this map.

Returns a read-only Set of all key/value pairs in this map.

Returns a read-only Set of all key/value pairs in this map.

Returns a read-only Set of all key/value pairs in this map.

Returns a read-only Set of all key/value pairs in this map.

Returns a read-only Set of all key/value pairs in this map.

Returns a read-only Set of all keys in this map.

Returns a read-only Set of all keys in this map.

Returns a read-only Set of all keys in this map.

Returns a read-only Set of all keys in this map.

Returns a read-only Set of all keys in this map.

Returns a read-only Set of all keys in this map.

expect abstract val size: Int

Returns the number of key/value pairs in the map.

actual abstract val size: Int

Returns the number of key/value pairs in the map.

actual abstract val size: Int

Returns the number of key/value pairs in the map.

actual abstract val size: Int

Returns the number of key/value pairs in the map.

actual abstract val size: Int

Returns the number of key/value pairs in the map.

actual abstract val size: Int

Returns the number of key/value pairs in the map.

Returns a read-only Collection 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. Note that this collection may contain duplicate values.

Returns a read-only Collection 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. Note that this collection may contain duplicate values.

Returns a read-only Collection 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. Note that this collection may contain duplicate values.

Returns true if all entries match the given predicate.

Returns true if map has at least one entry.

Returns true if at least one entry matches the given predicate.

Creates an Iterable instance that wraps the original map returning its entries when being iterated.

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.

Creates a Sequence instance that wraps the original map returning its entries when being iterated.

Checks if the map contains the given 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 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.

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 the number of entries in this map.

Returns the number of entries matching the given predicate.

Returns a new map containing all key-value pairs matching the given predicate.

Returns a map containing all key-value pairs with keys matching the given predicate.

Returns a new map containing all key-value pairs not matching the given predicate.

Appends all entries matching the given predicate into the mutable map given as destination parameter.

Returns a map containing all key-value pairs with values matching the given predicate.

Returns the first non-null value produced by transform function being applied to entries of this map in iteration order, or throws NoSuchElementException if no non-null value was produced.

Returns the first non-null value produced by transform function being applied to entries of this map in iteration order, or null if no non-null value was produced.

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.

Performs the given action on each entry.

expect abstract operator fun get(key: K): V?

Returns the value corresponding to the given key, or null if such a key is not present in the map.

actual abstract operator fun get(key: K): V?

Returns the value corresponding to the given key, or null if such a key is not present in the map.

actual abstract operator fun get(key: K): V?

Returns the value corresponding to the given key, or null if such a key is not present in the map.

actual abstract operator fun get(key: K): V?

Returns the value corresponding to the given key, or null if such a key is not present in the map.

actual abstract operator fun get(key: K): V?

Returns the value corresponding to the given key, or null if such a key is not present in the map.

actual abstract operator fun get(key: K): V?

Returns the value corresponding to the given key, or null if such a key is not present in the map.

inline operator fun <K, V> Map<out K, V>.get(key: K): V?

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 if the value is present and not null. Otherwise, returns the result of the defaultValue function.

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 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 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 this map is not empty.

Returns true if this nullable map is either null or empty.

Returns a list containing the results of applying the given transform function to each entry in the original map.

Returns a new 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 obtained by applying the transform function to each entry in this Map and the values of this map.

Returns a list containing only the non-null results of applying the given transform function to each entry in the original map.

Applies the given transform function to each entry in the original map and appends only the non-null results to the given destination.

Applies the given transform function to each entry of the original map and appends the results to the given destination.

Returns a new map with entries having the keys of this map and the values obtained by applying the transform function to each entry in 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 first entry yielding the largest value of the given selector function.

Returns the first entry yielding the largest value of the given selector function or null if there are no entries.

Returns the largest value among all values produced by selector function applied to each entry in the map.

Returns the largest value among all values produced by selector function applied to each entry in the map or null if the map is empty.

Returns the largest value according to the provided comparator 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.

Returns the first entry having the largest value according to the provided comparator or null if there are no entries.

Returns the first entry yielding the smallest value of the given selector function.

Returns the first entry yielding the smallest value of the given selector function 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 among all values produced by selector function applied to each entry in the map or null if the map is empty.

Returns the smallest value according to the provided comparator 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.

Returns the first entry having the smallest value according to the provided comparator.

Returns the first entry having the smallest value according to the provided comparator or null if there are no entries.

Returns true if the map has no entries.

Returns true if no entries match the given predicate.

Returns the Map if its not null, or the empty Map otherwise.

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.

Returns a List containing all key-value pairs.

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