Defining a map in Kotlin
No need for explicitly defining generic types and no repetitive put
or assignment to define maps.
1
2
3
4
5
6
7
8
9
10
11
12
fun createImplicitMap(): Map<String, String>{
return mapOf(
"keyA" to "valueA",
"keyB" to "valueB",
"keyC" to "valueC"
)
}
fun main(args: Array<String>) {
println(createImplicitMap())
// {keyA=valueA, keyB=valueB, keyC=valueC}
}
From time to time, there is a need to define a hardcoded Map
. There are several ways to do them in Java, but nothing gets closer to the beauty of Kotlin.