0
0
Kotlinprogramming~15 mins

Associate for map creation in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Associate for map creation
📖 Scenario: You work in a small bookstore. You have a list of book titles and their prices. You want to create a map where each book title is linked to its price for easy lookup.
🎯 Goal: Build a Kotlin program that creates a map from a list of book titles and a list of prices using the associate function.
📋 What You'll Learn
Create a list of book titles called books with these exact values: "Kotlin Basics", "Advanced Kotlin", "Kotlin Coroutines"
Create a list of prices called prices with these exact values: 29.99, 39.99, 34.99
Create a map called bookPrices by associating each book title with its corresponding price using associate
Print the bookPrices map
💡 Why This Matters
🌍 Real World
Creating maps from lists is useful when you want to link related data, like product names and prices, for quick access.
💼 Career
Many jobs require handling data collections and transforming them into maps or dictionaries for efficient lookup and processing.
Progress0 / 4 steps
1
Create the book titles list
Create a list called books with these exact values: "Kotlin Basics", "Advanced Kotlin", "Kotlin Coroutines"
Kotlin
Need a hint?

Use listOf to create a list with the given book titles.

2
Create the prices list
Create a list called prices with these exact values: 29.99, 39.99, 34.99
Kotlin
Need a hint?

Use listOf to create a list with the given prices.

3
Create the map using associate
Create a map called bookPrices by associating each book title in books with its corresponding price in prices using associate and withIndex()
Kotlin
Need a hint?

Use withIndex() on books to get index and book, then associate each book with the price at the same index.

4
Print the map
Print the bookPrices map using println
Kotlin
Need a hint?

Use println(bookPrices) to display the map.