W3cubDocs

/Kotlin

mutableMapOf

Platform and version requirements: JVM (1.1), JS (1.1), Native (1.1)
fun <K, V> mutableMapOf(): MutableMap<K, V>

Returns an empty new MutableMap.

The returned map preserves the entry iteration order.

import kotlin.test.*
import java.util.*

fun main(args: Array<String>) {
//sampleStart
val map = mutableMapOf<Int, Any?>()
println("map.isEmpty() is ${map.isEmpty()}") // true

map[1] = "x"
map[2] = 1.05
// Now map contains something:
println(map) // {1=x, 2=1.05}
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <K, V> mutableMapOf(
    vararg pairs: Pair<K, V>
): MutableMap<K, V>

Returns a new MutableMap with the specified contents, given as a list of pairs where the first component is the key and the second is the value.

If multiple pairs have the same key, the resulting map will contain the value from the last of those pairs.

Entries of the map are iterated in the order they were specified.

import kotlin.test.*
import java.util.*

fun main(args: Array<String>) {
//sampleStart
val map = mutableMapOf(1 to "x", 2 to "y", -1 to "zz")
println(map) // {1=x, 2=y, -1=zz}

map[1] = "a"
println(map) // {1=a, 2=y, -1=zz}
//sampleEnd
}
import kotlin.test.*
import java.util.*

fun main(args: Array<String>) {
//sampleStart
val map = mutableMapOf<Int, Any?>()
println("map.isEmpty() is ${map.isEmpty()}") // true

map[1] = "x"
map[2] = 1.05
// Now map contains something:
println(map) // {1=x, 2=1.05}
//sampleEnd
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-map-of.html