Left Index

Kotlin mutableMapOf()

In Kotlin, map is a collection containing data in pairs, which consists of key and value, and key are always unique and one key hold one value only. mapOf() create Immutable(i.e. read only) maps and mutableMapOf() create Mutable(i.e. can be modify) maps. Let's create example of mapOf():

Example:
package com.theitbulls.kotlinex

import java.util.ArrayList

fun main(args: Array<String>) {
	var mapOf = mutableMapOf<Int, String>(1 to "John", 2 to "Tony", 3 to "Denial")

	// adding key pair to mutableMapOf
	mapOf.put(4, "Jony")
	mapOf.forEach { item -> println("Key: ${item.key}, Value: ${item.value}") }
}
Output:
Key: 1, Value: John
Key: 2, Value: Tony
Key: 3, Value: Denial
Key: 4, Value: Jony

You can find how to iterate, get etc... Click Here