How to Create a Map of Maps in Kotlin - Step by Step Examples
How to Create a Map of Maps in Kotlin ?
Answer
To create a map of maps in Kotlin, you can use the Map and MutableMap data structures. A map of maps allows you to organize data hierarchically, where each key in the outer map points to another map.
✐ Examples
1 Creating a Map of Maps with Integer Keys and Values
We can create a map of maps in Kotlin by using the Map and MutableMap data structures. This example demonstrates creating a map where both the outer and inner maps use integers for keys and values.
For example,
- We start by declaring and initializing a mutable map named
outerMap
where each value is another mutable map. - We populate the inner maps with key-value pairs.
- We insert the inner maps into the outer map using appropriate keys.
- We print the resulting map of maps to the console to verify the structure.
Kotlin Program
fun main() {
// Declare a map of maps
val outerMap: MutableMap<Int, MutableMap<Int, Int>> = mutableMapOf()
// Populate the inner maps
val innerMap1 = mutableMapOf(1 to 10, 2 to 20, 3 to 30)
val innerMap2 = mutableMapOf(4 to 40, 5 to 50, 6 to 60)
// Insert the inner maps into the outer map
outerMap[1] = innerMap1
outerMap[2] = innerMap2
// Print the resulting map of maps
println("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")
}
}
}
Output
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
2 Creating a Map of Maps with Mixed Key and Value Types
We can create a map of maps in Kotlin by using the Map and MutableMap data structures. This example demonstrates creating a map where the outer map uses strings for keys and the inner maps use integers for keys and strings for values.
For example,
- We start by declaring and initializing a mutable map named
outerMap
where each value is another mutable map with mixed key and value types. - We populate the inner maps with key-value pairs.
- We insert the inner maps into the outer map using appropriate keys.
- We print the resulting map of maps to the console to verify the structure.
Kotlin Program
fun main() {
// Declare a map of maps
val outerMap: MutableMap<String, MutableMap<Int, String>> = mutableMapOf()
// Populate the inner maps
val innerMap1 = mutableMapOf(1 to "one", 2 to "two", 3 to "three")
val innerMap2 = mutableMapOf(4 to "four", 5 to "five", 6 to "six")
// Insert the inner maps into the outer map
outerMap["first"] = innerMap1
outerMap["second"] = innerMap2
// Print the resulting map of maps
println("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")
}
}
}
Output
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
3 Creating a Map of Maps with String Keys and Double Values
We can create a map of maps in Kotlin by using the Map and MutableMap data structures. This example demonstrates creating a map where both the outer and inner maps use strings for keys and doubles for values.
For example,
- We start by declaring and initializing a mutable map named
outerMap
where each value is another mutable map with strings for keys and doubles for values. - We populate the inner maps with key-value pairs.
- We insert the inner maps into the outer map using appropriate keys.
- We print the resulting map of maps to the console to verify the structure.
Kotlin Program
fun main() {
// Declare a map of maps
val outerMap: MutableMap<String, MutableMap<String, Double>> = mutableMapOf()
// Populate the inner maps
val innerMap1 = mutableMapOf("A" to 1.1, "B" to 2.2, "C" to 3.3)
val innerMap2 = mutableMapOf("D" to 4.4, "E" to 5.5, "F" to 6.6)
// Insert the inner maps into the outer map
outerMap["group1"] = innerMap1
outerMap["group2"] = innerMap2
// Print the resulting map of maps
println("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")
}
}
}
Output
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
Summary
In this tutorial, we learned How to Create a Map of Maps in Kotlin language with well detailed examples.
More Kotlin Maps Tutorials
- How to create an Empty Map in Kotlin ?
- How to create a Map with Initial Key-Value Pairs in Kotlin ?
- How to Print a Map in Kotlin ?
- How to Add a Key-Value Pair to a Map in Kotlin ?
- How to Set a Default Value for a Key in a Map in Kotlin ?
- How to Update the Value for a Key in a Map in Kotlin ?
- How to Check if a Map is Empty in Kotlin ?
- How to Check if a Key Exists in a Map in Kotlin ?
- How to Check if a Value Exists in a Map in Kotlin ?
- How to Get the Value Associated with a Key in a Map in Kotlin ?
- How to Remove a Key-Value Pair from a Map in Kotlin ?
- How to Remove Key-Value Pairs from a Map Based on Values in Kotlin ?
- How to Clear All Key-Value Pairs from a Map in Kotlin ?
- How to Iterate Over Keys in a Map in Kotlin ?
- How to Iterate Over Values in a Map in Kotlin ?
- How to Iterate Over Entries (Key-Value Pairs) in a Map in Kotlin ?
- How to Get the Size (Number of Key-Value Pairs) of a Map in Kotlin ?
- How to Convert a Map to an Array of Keys in Kotlin ?
- How to Convert a Map to an Array of Values in Kotlin ?
- How to Convert a Map to an Array of Key-Value Pairs in Kotlin ?
- How to Merge Two Maps in Kotlin ?
- How to Copy a Map in Kotlin ?
- How to Check if Two Maps are Equal in Kotlin ?
- How to Sort a Map by Keys in Kotlin ?
- How to Sort a Map by Values in Kotlin ?
- How to Filter a Map Based on Keys in Kotlin ?
- How to Filter a Map Based on Values in Kotlin ?
- How to Reduce Values in a Map to a Single Value in Kotlin ?
- How to Convert an Array of Key-Value Pairs to a Map in Kotlin ?
- How to Convert a Map to a JSON String in Kotlin ?
- How to Convert a JSON String to a Map in Kotlin ?
- How to Swap Keys and Values in a Map in Kotlin ?
- How to Create a Map of Maps in Kotlin ?
- How to Iterate Over a Map of Maps in Kotlin ?