Lesson 4
Applying Maps in Real-World Scenarios with Kotlin
Introduction and Goal Setting

Hello there! In this lesson, we will apply Maps to real-world challenges. Our focus will be on solving tasks such as cataloging books in a library, counting votes in an election, and tracking inventories.

Real-World Scenarios Calling for Maps

Maps are beneficial in real-life applications, such as the ones mentioned above, due to their ability to rapidly retrieve data with unique keys and efficiently handle larger datasets. Let's understand their efficiency with some actual examples.

Solving Real-World Task 1: Cataloging Books in a Library

Suppose you're asked to manage the cataloging of books in a library. Here, the book ID serves as the key, while the details of the book, such as the title, author, and year of publication, are stored as values.

This approach allows us to add, search for, and remove books from our library catalog using just a few lines of Kotlin code.

Kotlin
1fun main() { 2 // Initializing a Map 3 val libraryCatalog = mutableMapOf<String, MutableMap<String, String>>() 4 5 // Details of a book 6 val bookId = "123" 7 // Creating a Map to store details of the book 8 val bookDetails = mutableMapOf( 9 "title" to "To Kill a Mockingbird", 10 "author" to "Harper Lee", 11 "year_published" to "1960" 12 ) 13 14 libraryCatalog[bookId] = bookDetails // Adding a book to library catalog 15 16 // Searching for a book 17 if (libraryCatalog.containsKey(bookId)) { 18 val details = libraryCatalog[bookId] 19 println("Title: ${details?.get("title")}, Author: ${details?.get("author")}, Year Published: ${details?.get("year_published")}") 20 } 21 22 libraryCatalog.remove(bookId) // Removing a book from the library 23}

As you can see, Maps make the task of cataloging books in the library simpler and more efficient!

Solving Real-World Task 2: Counting Votes in an Election

Imagine a scenario in which we need to count votes in an election. We employ a Map, where each name is a unique key, and the frequency of that name serves as the associated value. Let's write some Kotlin code to better understand this.

Kotlin
1fun main() { 2 val votesList = listOf("Alice", "Bob", "Alice", "Charlie", "Bob", "Alice") // Cast votes 3 val voteCounts = mutableMapOf<String, Int>() // Initializing a Map 4 5 // Counting the votes 6 for (name in votesList) { 7 voteCounts[name] = voteCounts.getOrDefault(name, 0) + 1 8 } 9 10 for ((key, value) in voteCounts) { 11 println("$key: $value") 12 } 13 // Prints: Alice: 3, Bob: 2, Charlie: 1 14}

Maps facilitate the efficient counting of votes.

Solving Real-World Task 3: Tracking Store Inventories

Finally, consider a task that involves managing a store's inventory. Here, we can use a Map in which product names are keys and quantities are values. This approach allows us to easily add new items, adjust the quantity of items, check whether an item is in stock, and much more.

Kotlin
1fun main() { 2 val storeInventory = mutableMapOf("Apples" to 100, "Bananas" to 80, "Oranges" to 90) // Initializing an inventory 3 4 storeInventory["Pears"] = 50 // Adding a new product to inventory and setting its quantity 5 6 storeInventory["Apples"] = storeInventory["Apples"]!! + 20 // Updating the number of apples in inventory 7 8 var prod = "Apples" // A product to be checked 9 println("Total $prod in stock: ${storeInventory[prod]}") 10 11 // Check if a product is in stock 12 prod = "Mangoes" 13 if (prod in storeInventory) { // If mangoes exist in inventory 14 println("$prod are in stock.") 15 } else { // If mangoes don't exist in inventory 16 println("$prod are out of stock.") 17 } 18}

Thus, when managing inventory data, Maps offer an efficient solution!

Lesson Summary and Practice

In this lesson, we bridged the gap between the theory of Maps and their practical applications in Kotlin. We explored real-world problems that can be solved using Maps and implemented Kotlin code to address them.

Now, get ready for hands-on practice exercises that will help reinforce these concepts and hone your Map problem-solving skills using Kotlin-specific syntax. Happy coding!

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