1. Lists, Sets and Maps at a Glance

[THEORY]

[PREDICT]

Read the code carefully before you run it.

prediction.kt
fun main() {
    val categories = listOf("Tools", "Electronics", "Tools", "Office")
    val categoriesSet = categories.toSet()
    println(categories.size)
    println(categoriesSet.size)
}

Which two numbers does this program print?

[TASK]

A warehouse management system manages products. Outside main() (top level, which works for val just as well as inside a function) create three declarations: val productNames as a List<String> with the entries "Screwdriver", "Hammer", "Screwdriver", "Pliers" (the item "Screwdriver" really is in stock twice, so it is deliberately duplicated). val categories as a Set<String> with the entries "Tools", "Tools", "Electronics" — use setOf for that. val prices as a Map<String, Double> that maps the product "Hammer" to the price 12.5 and "Pliers" to the price 8.0. Finally print productNames.size, categories.size and prices["Hammer"] in main() (three println calls).

task.kt
// TODO: create val productNames as List<String> with the four product names (via listOf) // TODO: create val categories as Set<String> with the three categories (via setOf) // TODO: create val prices as Map<String, Double> with the two prices (via mapOf) fun main() { // TODO: print productNames.size, categories.size and prices["Hammer"] (three println calls) }

Before you get a hint: what have you tried, and where exactly are you stuck?

[COMPREHENSION CHECK]

1. What does setOf("A", "A", "B") return?

2. Which statement about Map is true?

3. What is true about the relationship between List, Set and Map?