How to create a map from an array in Kotlin?
In below example we are going to use an array calledarrayRules
, Arrays in Kotlin has e function called .map()
which can be used to split based on ->
fun main(args: Array<String>) {
val arrayRules = arrayOf(
"A -> AA",
"B -> AB",
"C -> AC",
"D -> AD",
"E -> AE",
)
val result = arrayRules
.map { it.split(" -> ".toRegex()) }
.associate { it.first() to it.last() }
println(result)
}
Output
{A=AA, B=AB, C=AC, D=AD, E=AE}