mapOf

fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V>(source)

Returns a new read-only map with the specified contents, given as a list of pairs where the first value 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.

The returned map is serializable (JVM).

Since Kotlin

1.0

Samples

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

fun main() { 
   //sampleStart 
   val map = mapOf(1 to "x", 2 to "y", -1 to "zz")
println(map) // {1=x, 2=y, -1=zz} 
   //sampleEnd
}

inline fun <K, V> mapOf(): Map<K, V>(source)

Returns an empty read-only map.

The returned map is serializable (JVM).

Since Kotlin

1.0

Samples

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

fun main() { 
   //sampleStart 
               val map = emptyMap<String, Int>()
            println("map.isEmpty() is ${map.isEmpty()}") // true

            val anotherMap = mapOf<String, Int>()
            // "Empty maps are equal"
println("map == anotherMap is ${map == anotherMap}") // true 
   //sampleEnd
}

expect fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V>(source)

Returns a new read-only map, mapping only the specified key to the specified value.

The returned map is serializable (JVM).

Since Kotlin

1.9

Samples

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

fun main() { 
   //sampleStart 
   val map = mapOf(1 to "x", 2 to "y", -1 to "zz")
println(map) // {1=x, 2=y, -1=zz} 
   //sampleEnd
}
actual fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V>(source)

Returns a new read-only map, mapping only the specified key to the specified value.

Since Kotlin

1.1

Samples

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

fun main() { 
   //sampleStart 
   val map = mapOf(1 to "x", 2 to "y", -1 to "zz")
println(map) // {1=x, 2=y, -1=zz} 
   //sampleEnd
}
actual fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V>(source)

Returns a new read-only map, mapping only the specified key to the specified value.

The returned map is serializable.

Since Kotlin

1.0

Samples

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

fun main() { 
   //sampleStart 
   val map = mapOf(1 to "x", 2 to "y", -1 to "zz")
println(map) // {1=x, 2=y, -1=zz} 
   //sampleEnd
}
actual fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V>(source)

Returns a new read-only map, mapping only the specified key to the specified value.

Since Kotlin

1.9

Samples

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

fun main() { 
   //sampleStart 
   val map = mapOf(1 to "x", 2 to "y", -1 to "zz")
println(map) // {1=x, 2=y, -1=zz} 
   //sampleEnd
}
actual fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V>(source)

Returns a new read-only map, mapping only the specified key to the specified value.

Since Kotlin

1.9

Samples

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

fun main() { 
   //sampleStart 
   val map = mapOf(1 to "x", 2 to "y", -1 to "zz")
println(map) // {1=x, 2=y, -1=zz} 
   //sampleEnd
}
actual fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V>(source)

Returns a new read-only map, mapping only the specified key to the specified value.

Since Kotlin

1.9

Samples

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

fun main() { 
   //sampleStart 
   val map = mapOf(1 to "x", 2 to "y", -1 to "zz")
println(map) // {1=x, 2=y, -1=zz} 
   //sampleEnd
}