Kotlin MutableMap Tutorial 🎯

beginner
8 min

Kotlin MutableMap Tutorial 🎯

Welcome to our deep dive into Kotlin's MutableMap! In this comprehensive guide, we'll learn how to create, manipulate, and use mutable maps in your projects. By the end of this lesson, you'll be comfortable working with MutableMap in a variety of scenarios. 📝

What is a Map? 📝

A Map is a collection of key-value pairs in Kotlin. Each key is unique, and it corresponds to a value. The values can be of any type, making maps incredibly versatile for storing and organizing data. 💡 Pro Tip: Maps are often used when you need to associate multiple values with a single key.

MutableMap vs. ImmutableMap 💡

In Kotlin, there are two types of maps: MutableMap and ImmutableMap. The primary difference is that MutableMap can be modified, whereas ImmutableMap is read-only. We'll focus on MutableMap in this tutorial.

Creating a MutableMap 💡

There are multiple ways to create a MutableMap in Kotlin. We'll explore three common methods:

  1. Using the hashMapOf() function
kotlin
val myMutableMap = hashMapOf<String, Int>( "apple" to 10, "banana" to 20, "orange" to 30 )
  1. Using the mutableMapOf() function
kotlin
val myMutableMap = mutableMapOf<String, Int>() myMutableMap["apple"] = 10 myMutableMap["banana"] = 20 myMutableMap["orange"] = 30
  1. Using the put() function to add key-value pairs
kotlin
val myMutableMap = mutableMapOf<String, Int>() myMutableMap.put("apple", 10) myMutableMap.put("banana", 20) myMutableMap.put("orange", 30)

Accessing and Manipulating a MutableMap 💡

To access a value in a MutableMap, use the key associated with the value:

kotlin
println(myMutableMap["apple"]) // Output: 10

To update a value in a MutableMap, assign a new value to the key:

kotlin
myMutableMap["apple"] = 20 println(myMutableMap["apple"]) // Output: 20

To remove a key-value pair from a MutableMap, use the remove() function:

kotlin
myMutableMap.remove("banana") println(myMutableMap) // Output: {apple=20, orange=30}

Quiz 📝

Quick Quiz
Question 1 of 1

How can you create a `MutableMap` using the `hashMapOf()` function?

Practical Examples 🎯

💡 Pro Tip: Using MutableMap in a Simple Shopping Cart

kotlin
val shoppingCart = mutableMapOf<String, Int>() // Add items to the shopping cart shoppingCart["apple"] = 5 shoppingCart["banana"] = 3 shoppingCart["orange"] = 2 // Update item quantity shoppingCart["apple"] = 7 // Remove an item shoppingCart.remove("banana") // Calculate the total cost val totalCost = shoppingCart.values.sum() println("Total cost: $totalCost")

💡 Pro Tip: Using MutableMap for Storing User Data

kotlin
val users = mutableMapOf<String, User>() // Add user data val user1 = User("John Doe", 30) val user2 = User("Jane Smith", 28) users["John Doe"] = user1 users["Jane Smith"] = user2 // Update user data user1.age = 31 users["John Doe"] = user1 // Access user data println(users["John Doe"]) // Output: User(name=John Doe, age=31)

Conclusion 💡

With a solid understanding of MutableMap in Kotlin, you're now ready to take on a variety of projects that require key-value data management. As you continue to learn and grow, don't forget to explore other collection types like List and Set! Happy coding! 🎯