0
0
Kotlinprogramming~20 mins

Map creation (mapOf, mutableMapOf) in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Create and Use Maps with mapOf and mutableMapOf in Kotlin
📖 Scenario: You are organizing a small library. You want to keep track of book titles and their authors using a map.
🎯 Goal: Build a Kotlin program that creates a map of books and authors, adds a new book, and then prints all entries.
📋 What You'll Learn
Create an immutable map using mapOf with given book-author pairs
Create a mutable map using mutableMapOf from the immutable map
Add a new book and author to the mutable map
Print all book titles and authors from the mutable map
💡 Why This Matters
🌍 Real World
Maps are useful to store pairs of related data, like book titles and authors, product names and prices, or usernames and emails.
💼 Career
Understanding how to create and manipulate maps is important for many programming jobs, especially when working with data collections and key-value pairs.
Progress0 / 4 steps
1
Create an immutable map of books and authors
Create an immutable map called books using mapOf with these exact entries: "1984" to "George Orwell", "Brave New World" to "Aldous Huxley", and "Fahrenheit 451" to "Ray Bradbury".
Kotlin
Need a hint?

Use mapOf with pairs like "key" to "value" inside parentheses.

2
Create a mutable map from the immutable map
Create a mutable map called mutableBooks using mutableMapOf and initialize it with the entries from the books map.
Kotlin
Need a hint?

Create an empty mutable map with mutableMapOf<String, String>() and then add all entries from books using putAll.

3
Add a new book and author to the mutable map
Add a new entry to mutableBooks with the key "The Hobbit" and the value "J.R.R. Tolkien".
Kotlin
Need a hint?

Use the bracket notation mutableBooks["The Hobbit"] = "J.R.R. Tolkien" to add a new entry.

4
Print all book titles and authors
Use a for loop with variables title and author to iterate over mutableBooks.entries and print each book and author in the format: "Title: [title], Author: [author]".
Kotlin
Need a hint?

Use a for loop with for ((title, author) in mutableBooks.entries) and print with println("Title: $title, Author: $author").