MutableMap
interface MutableMap<K, V> : Map<K, V>
A modifiable collection that holds pairs of objects (keys and values) and supports efficiently retrieving the value corresponding to each key. Map keys are unique; the map holds only one value for each key.
Parameters
K
- the type of map keys. The map is invariant on its key type.
V
- the type of map values. The mutable map is invariant on its value type.
Types
MutableEntry
Represents a key/value pair held by a MutableMap.
interface MutableEntry<K, V> : Entry<K, V>
Properties
entries
Returns a MutableSet of all key/value pairs in this map.
abstract val entries: MutableSet<MutableEntry<K, V>>
keys
Returns a MutableSet of all keys in this map.
abstract val keys: MutableSet<K>
values
Returns a MutableCollection of all values in this map. Note that this collection may contain duplicate values.
abstract val values: MutableCollection<V>
Functions
clear
Removes all elements from this map.
abstract fun clear()
remove
Removes the specified key and its corresponding value from this map.
abstract fun remove(key: K): V?
Removes the entry for the specified key only if it is mapped to the specified value.
open fun remove(key: K, value: V): Boolean
Extension Functions
contains
Checks if the map contains the given key.
operator fun <K, V> Map<out K, V>.contains(key: K): Boolean
filterNotTo
Appends all entries not matching the given predicate into the given destination.
fun <K, V, M : MutableMap<in K, in V>> Map<out K, V>.filterNotTo(
destination: M,
predicate: (Entry<K, V>) -> Boolean
): M
filterTo
Appends all entries matching the given predicate into the mutable map given as destination parameter.
fun <K, V, M : MutableMap<in K, in V>> Map<out K, V>.filterTo(
destination: M,
predicate: (Entry<K, V>) -> Boolean
): M
flatMapTo
Appends all elements yielded from results of transform function being invoked on each entry of original map, to the given destination.
fun <K, V, R, C : MutableCollection<in R>> Map<out K, V>.flatMapTo(
destination: C,
transform: (Entry<K, V>) -> Iterable<R>
): C
getOrElse
Returns the value for the given key, or the result of the defaultValue function if there was no entry for the given key.
fun <K, V> Map<K, V>.getOrElse(
key: K,
defaultValue: () -> V
): V
getOrPut
Returns the value for the given key. If the key is not found in the map, calls the defaultValue function, puts its result into the map under the given key and returns it.
fun <K, V> MutableMap<K, V>.getOrPut(
key: K,
defaultValue: () -> V
): V
getValue
Returns the value of the property for the given object from this mutable map.
operator fun <V, V1 : V> MutableMap<in String, out V>.getValue(
thisRef: Any?,
property: KProperty<*>
): V1
ifEmpty
Returns this map if it's not empty or the result of calling defaultValue function if the map is empty.
fun <M, R> M.ifEmpty(
defaultValue: () -> R
): R where M : Map<*, *>, M : R
isNotEmpty
Returns true
if this map is not empty.
fun <K, V> Map<out K, V>.isNotEmpty(): Boolean
isNullOrEmpty
Returns true
if this nullable map is either null or empty.
fun <K, V> Map<out K, V>?.isNullOrEmpty(): Boolean
iterator
Returns a MutableIterator over the mutable entries in the MutableMap.
operator fun <K, V> MutableMap<K, V>.iterator(): MutableIterator<MutableEntry<K, V>>
mapKeysTo
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.
fun <K, V, R, M : MutableMap<in R, in V>> Map<out K, V>.mapKeysTo(
destination: M,
transform: (Entry<K, V>) -> R
): M
mapNotNullTo
Applies the given transform function to each entry in the original map and appends only the non-null results to the given destination.
fun <K, V, R : Any, C : MutableCollection<in R>> Map<out K, V>.mapNotNullTo(
destination: C,
transform: (Entry<K, V>) -> R?
): C
mapTo
Applies the given transform function to each entry of the original map and appends the results to the given destination.
fun <K, V, R, C : MutableCollection<in R>> Map<out K, V>.mapTo(
destination: C,
transform: (Entry<K, V>) -> R
): C
mapValuesTo
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.
fun <K, V, R, M : MutableMap<in K, in R>> Map<out K, V>.mapValuesTo(
destination: M,
transform: (Entry<K, V>) -> R
): M
maxBy
Returns the first entry yielding the largest value of the given function or null
if there are no entries.
fun <K, V, R : Comparable<R>> Map<out K, V>.maxBy(
selector: (Entry<K, V>) -> R
): Entry<K, V>?
maxWith
Returns the first entry having the largest value according to the provided comparator or null
if there are no entries.
fun <K, V> Map<out K, V>.maxWith(
comparator: Comparator<in Entry<K, V>>
): Entry<K, V>?
minBy
Returns the first entry yielding the smallest value of the given function or null
if there are no entries.
fun <K, V, R : Comparable<R>> Map<out K, V>.minBy(
selector: (Entry<K, V>) -> R
): Entry<K, V>?
minus
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 collection.
Returns a map containing all entries of the original map except those entries the keys of which are contained in the given keys array.
minusAssign
Removes the entry with the given key from this mutable map.
operator fun <K, V> MutableMap<K, V>.minusAssign(key: K)
Removes all entries the keys of which are contained in the given keys collection from this mutable map.
operator fun <K, V> MutableMap<K, V>.minusAssign(
keys: Iterable<K>)
Removes all entries the keys of which are contained in the given keys array from this mutable map.
operator fun <K, V> MutableMap<K, V>.minusAssign(
keys: Array<out K>)
Removes all entries from the keys of which are contained in the given keys sequence from this mutable map.
operator fun <K, V> MutableMap<K, V>.minusAssign(
keys: Sequence<K>)
minWith
Returns the first entry having the smallest value according to the provided comparator or null
if there are no entries.
fun <K, V> Map<out K, V>.minWith(
comparator: Comparator<in Entry<K, V>>
): Entry<K, V>?
plus
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 a given array of key-value pairs.
Creates a new read-only map by replacing or adding entries to this map from a given sequence of key-value pairs.
plusAssign
Appends or replaces the given pair in this mutable map.
operator fun <K, V> MutableMap<in K, in V>.plusAssign(
pair: Pair<K, V>)
Appends or replaces all pairs from the given collection of pairs in this mutable map.
operator fun <K, V> MutableMap<in K, in V>.plusAssign(
pairs: Iterable<Pair<K, V>>)
Appends or replaces all pairs from the given array of pairs in this mutable map.
operator fun <K, V> MutableMap<in K, in V>.plusAssign(
pairs: Array<out Pair<K, V>>)
Appends or replaces all pairs from the given sequence of pairs in this mutable map.
operator fun <K, V> MutableMap<in K, in V>.plusAssign(
pairs: Sequence<Pair<K, V>>)
Appends or replaces all entries from the given map in this mutable map.
operator fun <K, V> MutableMap<in K, in V>.plusAssign(
map: Map<K, V>)
putAll
Puts all the given pairs into this MutableMap with the first component in the pair being the key and the second the value.
fun <K, V> MutableMap<in K, in V>.putAll(
pairs: Array<out Pair<K, V>>)
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.
fun <K, V> MutableMap<in K, in V>.putAll(
pairs: Iterable<Pair<K, V>>)
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.
fun <K, V> MutableMap<in K, in V>.putAll(
pairs: Sequence<Pair<K, V>>)
remove
Removes the specified key and its corresponding value from this map.
fun <K, V> MutableMap<out K, V>.remove(key: K): V?
Removes the entry for the specified key only if it is currently mapped to the specified value.
fun <K, V> MutableMap<out K, out V>.remove(
key: K,
value: V
): Boolean
set
Allows to use the index operator for storing values in a mutable map.
operator fun <K, V> MutableMap<K, V>.set(key: K, value: V)
setValue
Stores the value of the property for the given object in this mutable map.
operator fun <V> MutableMap<in String, in V>.setValue(
thisRef: Any?,
property: KProperty<*>,
value: V)
toMap
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.
fun <K, V, M : MutableMap<in K, in V>> Map<out K, V>.toMap(
destination: M
): M
toMutableMap
Returns a new mutable map containing all key-value pairs from the original map.
fun <K, V> Map<out K, V>.toMutableMap(): MutableMap<K, V>
toProperties
Converts this Map to a Properties object.
fun Map<String, String>.toProperties(): Properties
toSortedMap
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.
fun <K, V> Map<out K, V>.toSortedMap(
comparator: Comparator<in K>
): SortedMap<K, V>
withDefault
Returns a wrapper of this mutable map, having the implicit default value provided with the specified function defaultValue.
fun <K, V> MutableMap<K, V>.withDefault(
defaultValue: (key: K) -> V
): MutableMap<K, V>
Returns a wrapper of this read-only map, having the implicit default value provided with the specified function defaultValue.
Inheritors
AbstractMutableMap
Provides a skeletal implementation of the MutableMap interface.
abstract class AbstractMutableMap<K, V> : MutableMap<K, V>
abstract class AbstractMutableMap<K, V> :
MutableMap<K, V>,
AbstractMap<K, V>
abstract class AbstractMutableMap<K, V> :
AbstractMap<K, V>,
MutableMap<K, V>
HashMap
Hash table based implementation of the MutableMap interface.
class HashMap<K, V> : MutableMap<K, V>
typealias HashMap<K, V> = HashMap<K, V>
open class HashMap<K, V> :
AbstractMutableMap<K, V>,
MutableMap<K, V>
LinkedHashMap
Hash table based implementation of the MutableMap interface, which additionally preserves the insertion order of entries during the iteration.
class LinkedHashMap<K, V> : MutableMap<K, V>
typealias LinkedHashMap<K, V> = LinkedHashMap<K, V>
open class LinkedHashMap<K, V> :
HashMap<K, V>,
MutableMap<K, V>
typealias LinkedHashMap<K, V> = HashMap<K, V>