The syntax of Map.keys property is:
abstract val keys: Set<K>
This keys property of Map returns a read-only Set of all keys in this map.
In this example,
map
containing pairs of numbers and characters.keys
property.fun main(args: Array<String>) {
val map = mapOf(1 to 'a', 2 to 'b', 3 to 'c')
val keys = map.keys
println(keys)
}
[1, 2, 3]
In this example,
map
containing pairs of characters and numbers.keys
property.fun main(args: Array<String>) {
val map = mapOf('a' to 1, 'b' to 2, 'c' to 3)
val keys = map.keys
println(keys)
}
[a, b, c]
In this example,
map
containing pairs of strings and numbers.keys
property.fun main(args: Array<String>) {
val map = mapOf("apple" to 1, "banana" to 2, "cherry" to 3)
val keys = map.keys
println(keys)
}
[apple, banana, cherry]
In this Kotlin tutorial, we learned about keys property of Map: the syntax and few working examples with output and detailed explanation for each example.