Learn
Maps
Creating a Mutable Map

As we’ve seen with lists and sets, a map can be declared mutable or immutable. So far, we’ve only worked with maps whose entries are used for read-only operations. Let’s explore how to declare mutable maps and have more control of the data they hold.

To declare a mutable map, use the term, `mutableMapOf` in a map declaration:

``var/val mapName = mutableMapOf(key1 to val1, key2 to val2, key3 to val2)``

The above syntax can be used to declare the following mutable map of students and their age:

``var students = mutableMapOf("Willow" to 15, "Elijah" to 17, "Florence" to 16, "Muhammed" to 15) ``

Using the `[key]` syntax, we are able to retrieve and reassign key values. Assume Willow just celebrated her birthday and turned 16. We can update the age value as so:

``students["Willow"] = 16``

Notice how we’ve used the assignment operator to assign a new value to the key, `"Willow"`.

Note: In a mutable map, values can be updated, however, keys can never change. In order to update a key, the pair must be removed entirely and re-added. In the next exercise, we’ll see how to add and remove values from a map.

### Instructions

1.

In Shows.kt, declare a mutable map, `tvShows` that will store key-value pairs of a show’s name and its total number of episodes. Use the following information to populate your map:

• `"The Big Bang Theory"`: `278`
• `"Modern Family"`: `250`
• `"Bewitched"`: `254`
• `"That '70s Show"`: `200`
2.

The Big Bang Theory’s final, 279th episode, aired on May 16th, 2019. Update the value of this show in the map.

On the following line, wrap `tvShows` in a print statement to see the final map.