The syntax of Map.values property is:
abstract val values: Collection<V>
This values property of Map returns a read-only Collection of all values in this map. Note that this collection may contain duplicate values.
In this example,
map
with integer keys and character values.values
property of the map to get a read-only Collection of values.fun main(args: Array<String>) {
val map = mapOf(1 to 'a', 2 to 'b', 3 to 'c')
val values = map.values
println(values)
}
[a, b, c]
In this example,
map
with string keys and integer values.values
property of the map to get a read-only Collection of values.fun main(args: Array<String>) {
val map = mapOf("apple" to 1, "banana" to 2, "cherry" to 3)
val values = map.values
println(values)
}
[1, 2, 3]
In this example,
map
with integer keys and character values.for
loop to iterate over each value in map.values
.fun main(args: Array<String>) {
val map = mapOf(1 to 'a', 2 to 'b', 3 to 'c')
for (value in map.values) {
println(value)
}
}
a b c
In this Kotlin tutorial, we learned about values property of Map: the syntax and few working examples with output and detailed explanation for each example.