To iterate over a map of maps in Kotlin, you can use nested loops. The outer loop iterates over the outer map, and the inner loop iterates over the inner map.
We can iterate over a map of maps in Kotlin where both the outer and inner maps use integers for keys and values. This example demonstrates using nested loops to print each key-value pair.
For example,
outerMap
.fun main() {
// Declare and initialize a map of maps
val outerMap: Map<Int, Map<Int, Int>> = mapOf(
1 to mapOf(1 to 10, 2 to 20, 3 to 30),
2 to mapOf(4 to 40, 5 to 50, 6 to 60)
)
// Iterate over the map of maps
println("Iterating over map of maps with integer keys and values:")
for ((outerKey, innerMap) in outerMap) {
println("Outer key: $outerKey")
for ((innerKey, value) in innerMap) {
println(" Inner key: $innerKey, value: $value")
}
}
}
Iterating over map of maps with integer keys and values: Outer key: 1 Inner key: 1, value: 10 Inner key: 2, value: 20 Inner key: 3, value: 30 Outer key: 2 Inner key: 4, value: 40 Inner key: 5, value: 50 Inner key: 6, value: 60
We can iterate over a map of maps in Kotlin where the outer map uses strings for keys and the inner maps use integers for keys and strings for values. This example demonstrates using nested loops to print each key-value pair.
For example,
outerMap
with mixed key and value types.fun main() {
// Declare and initialize a map of maps
val outerMap: Map<String, Map<Int, String>> = mapOf(
"first" to mapOf(1 to "one", 2 to "two", 3 to "three"),
"second" to mapOf(4 to "four", 5 to "five", 6 to "six")
)
// Iterate over the map of maps
println("Iterating over map of maps with mixed key and value types:")
for ((outerKey, innerMap) in outerMap) {
println("Outer key: $outerKey")
for ((innerKey, value) in innerMap) {
println(" Inner key: $innerKey, value: $value")
}
}
}
Iterating over map of maps with mixed key and value types: Outer key: first Inner key: 1, value: one Inner key: 2, value: two Inner key: 3, value: three Outer key: second Inner key: 4, value: four Inner key: 5, value: five Inner key: 6, value: six
We can iterate over a map of maps in Kotlin where both the outer and inner maps use strings for keys and doubles for values. This example demonstrates using nested loops to print each key-value pair.
For example,
outerMap
with strings for keys and doubles for values.fun main() {
// Declare and initialize a map of maps
val outerMap: Map<String, Map<String, Double>> = mapOf(
"group1" to mapOf("A" to 1.1, "B" to 2.2, "C" to 3.3),
"group2" to mapOf("D" to 4.4, "E" to 5.5, "F" to 6.6)
)
// Iterate over the map of maps
println("Iterating over map of maps with string keys and double values:")
for ((outerKey, innerMap) in outerMap) {
println("Outer key: $outerKey")
for ((innerKey, value) in innerMap) {
println(" Inner key: $innerKey, value: $value")
}
}
}
Iterating over map of maps with string keys and double values: Outer key: group1 Inner key: A, value: 1.1 Inner key: B, value: 2.2 Inner key: C, value: 3.3 Outer key: group2 Inner key: D, value: 4.4 Inner key: E, value: 5.5 Inner key: F, value: 6.6
In this tutorial, we learned How to Iterate Over a Map of Maps in Kotlin language with well detailed examples.