The syntax of Map.entries property is:
abstract val entries: Set<Entry<K, V>>
This entries property of Map returns a read-only Set of all key/value pairs in this map.
In this example,
map
with integer keys and character values.entries
property of the map to get a read-only Set of key/value pairs.fun main(args: Array<String>) {
val map = mapOf(1 to 'a', 2 to 'b', 3 to 'c')
val entries = map.entries
println(entries)
}
[1=a, 2=b, 3=c]
In this example,
map
with string keys and integer values.entries
property of the map to get a read-only Set of key/value pairs.fun main(args: Array<String>) {
val map = mapOf("apple" to 1, "banana" to 2, "cherry" to 3)
val entries = map.entries
println(entries)
}
[apple=1, banana=2, cherry=3]
In this example,
map
with integer keys and character values.for
loop to iterate over each entry in map.entries
.key
and value
and print them to standard output.fun main(args: Array<String>) {
val map = mapOf(1 to 'a', 2 to 'b', 3 to 'c')
for ((key, value) in map.entries) {
println("Key: $key, Value: $value")
}
}
Key: 1, Value: a Key: 2, Value: b Key: 3, Value: c
In this Kotlin tutorial, we learned about entries property of Map: the syntax and few working examples with output and detailed explanation for each example.