The syntax of Map.size property is:
abstract val size: Int
This size property of Map returns the number of key/value pairs in the map.
In this example,
map
with integer keys and character values.size
property of the map to get the number of key/value pairs.fun main(args: Array<String>) {
val map = mapOf(1 to 'a', 2 to 'b', 3 to 'c')
val size = map.size
println(size)
}
3
In this example,
map
with string keys and integer values.size
property of the map to get the number of key/value pairs.fun main(args: Array<String>) {
val map = mapOf("apple" to 1, "banana" to 2, "cherry" to 3)
val size = map.size
println(size)
}
3
In this example,
map
with integer keys and character values.map
using string interpolation.fun main(args: Array<String>) {
val map = mapOf(1 to 'a', 2 to 'b', 3 to 'c')
println("Size of the map: $map.size")
}
Size of the map: 3
In this Kotlin tutorial, we learned about size property of Map: the syntax and few working examples with output and detailed explanation for each example.