Lesson 5
Navigating Kotlin Maps: Creation, Access, and Properties
Topic Overview and Actualization

Greetings, student! Today, we're studying the Map data structure in Kotlin. It functions like a dictionary, pairing a unique key with a corresponding value. Our primary focus will be on creating, accessing, and utilizing the unique properties of Maps in Kotlin.

Understanding Maps in Kotlin

In Kotlin, a Map stores key-value entries. Consider a dictionary, where words are keys, and definitions are values. This analogy parallels a Map's functioning: keys are unique, just as no two words share the same meaning.

Kotlin
1fun main() { 2 val animalHabitats = mapOf("Lion" to "Savannah", "Penguin" to "Antarctica", "Kangaroo" to "Australia") 3 println(animalHabitats) // prints {Lion=Savannah, Penguin=Antarctica, Kangaroo=Australia} 4}
Creating and Modifying Maps in Kotlin

In Kotlin, mapOf() and mutableMapOf() are functions designed for creating Maps, collections of key-value pairs. For mutable maps, you can insert or modify a key-value pair using the syntax map[<key>] = <value>. Modifying an existing key updates its associated value.

Kotlin
1fun main() { 2 val immutableMap = mapOf("Sam" to 23, "Amanda" to 30, "Trevor" to 29) 3 println(immutableMap) // prints {Sam=23, Amanda=30, Trevor=29} 4 5 val mutableMap = mutableMapOf("Mary" to 31, "Bob" to 28, "Hannah" to 27) 6 mutableMap["Roman"] = 27 // Adding a new key-value pair 7 mutableMap["Bob"] = 29 // Bob's age is updated to 29 8 println(mutableMap) // prints {Mary=31, Bob=29, Hannah=27, Roman=27} 9}

This example demonstrates not only how to define immutable and mutable maps but also how to dynamically add elements to mutable maps and modify existing entries.

Accessing and Modifying Elements in Maps

We access values in Maps via their keys. To add or remove elements, we respectively use the put() and remove() functions.

Kotlin
1fun main() { 2 val namesToAges = mapOf("Sam" to 23, "Amanda" to 30, "Trevor" to 29) 3 println(namesToAges["Amanda"]) // prints 30 4 5 val mutableNamesToAges = mutableMapOf("Sam" to 23, "Amanda" to 30) 6 mutableNamesToAges.put("Trevor", 29) 7 println(mutableNamesToAges) // prints {Sam=23, Amanda=30, Trevor=29} 8 9 mutableNamesToAges.remove("Sam") 10 println(mutableNamesToAges) // prints {Amanda=30, Trevor=29} 11}
Exploring Map Properties

Maps offer useful properties. size denotes the number of entries, keys returns all keys, and values list all values present in a Map.

Kotlin
1fun main() { 2 val namesToAges = mapOf("Sam" to 23, "Amanda" to 30, "Trevor" to 29) 3 println(namesToAges.size) // prints 3 4 println(namesToAges.keys) // prints [Sam, Amanda, Trevor] 5 println(namesToAges.values) // prints [23, 30, 29] 6 7 val emptyMap = mapOf<String, Int>() // Here we're defining a map where keys are strings and values are integers 8 println(emptyMap.isEmpty()) // prints true 9}
Lesson Summary and Practice

Brilliant! You've explored Maps in Kotlin and understood how to create, manage, and delve into their properties. This knowledge prepares you for the practice sessions ahead, reinforcing these concepts. Keep going!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.