Map — mapOf() / mutableMapOf()
In Kotlin, use mapOf() to create an immutable map of key-value pairs, and mutableMapOf() to create a mutable map. Use the to infix function to create pairs, and the [] operator or get() to retrieve values.
Syntax
// Create an immutable map
val capitals = mapOf("Japan" to "Tokyo", "France" to "Paris")
// Create a mutable map
val scores = mutableMapOf("Alice" to 85, "Bob" to 72)
scores["Carol"] = 91 // Add or update
scores.remove("Bob") // Remove
// Retrieve values
val tokyo = capitals["Japan"] // "Tokyo" (null if not found)
val default = capitals.getOrDefault("Germany", "Unknown") // "Unknown"
val safe = capitals.getOrElse("Germany") { "Unknown" } // "Unknown"
Method List
| Method / Property | Description |
|---|---|
| mapOf(k to v, ...) | Creates an immutable map. |
| mutableMapOf(k to v, ...) | Creates a mutable map. |
| map[key] | Returns the value for the key, or null if not found. |
| map.get(key) | Equivalent to []. |
| map.getOrDefault(key, default) | Returns the default value if the key is not found. |
| map.getOrElse(key) { lambda } | Returns the lambda result if the key is not found. |
| map.containsKey(key) | Checks whether the key exists (also writable as key in map). |
| map.keys / .values / .entries | Returns a collection of keys, values, or entries. |
| MutableMap[key] = value | Adds or updates a value. |
| MutableMap.remove(key) | Removes the key and its associated value. |
| MutableMap.putIfAbsent(key, value) | Adds the entry only if the key does not already exist. |
| map.map { (k, v) -> } | Returns a new list by transforming each entry. |
| map.filter { (k, v) -> } | Returns a map containing only entries that match the condition. |
Sample Code
fun main() {
// Basic mapOf
val capitals = mapOf(
"Japan" to "Tokyo",
"France" to "Paris",
"UK" to "London",
"Germany" to "Berlin"
)
// Key access
println(capitals["Japan"]) // Tokyo
println(capitals["Spain"]) // null
println(capitals.getOrDefault("Spain", "Unknown")) // Unknown
// Check existence
println("Japan" in capitals) // true
println(capitals.containsKey("China")) // false
println()
// keys / values / entries
println("Countries: ${capitals.keys}")
println("Capitals: ${capitals.values}")
capitals.entries.forEach { (country, capital) ->
println(" $country → $capital")
}
println()
// Mutable map
val inventory = mutableMapOf("Apple" to 10, "Banana" to 5)
inventory["Orange"] = 8 // Add
inventory["Apple"] = 15 // Update
inventory.remove("Banana") // Remove
println("Inventory: $inventory")
// putIfAbsent — adds only if the key does not exist
inventory.putIfAbsent("Apple", 999) // Already exists, so skipped
inventory.putIfAbsent("Grape", 3) // Added
println("After adding: $inventory")
println()
// Transform a map with map / filter
val prices = mapOf("Apple" to 150, "Banana" to 100, "Orange" to 200, "Grape" to 500)
// Keep only items priced over 150
val expensive = prices.filter { (_, price) -> price > 150 }
println("Over 150: $expensive")
// Create a new map with prices multiplied by 1.1
val raised = prices.mapValues { (_, price) -> (price * 1.1).toInt() }
println("After 10% increase: $raised")
}
Notes
Kotlin map operations support the same functional style as list operations. You can use mapValues() to transform only the values, or filter() to narrow down entries by key or value conditions.
Maps use Java's LinkedHashMap internally, so insertion order is preserved. Use sortedMapOf() if you need keys in alphabetical order.
For set operations, see Set — setOf() / mutableSetOf(). For list basics, see List — listOf() / mutableListOf().
If you find any errors or copyright issues, please contact us.