Bird
Raised Fist0
Android Kotlinmobile~20 mins

Collections (List, Map, Set) in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Collections Demo
This screen shows examples of Kotlin collections: List, Map, and Set. It displays each collection's content in a simple vertical list.
Target UI
-------------------------
| Collections Demo       |
|-----------------------|
| List:                 |
| - Apple               |
| - Banana              |
| - Cherry              |
|                       |
| Map:                  |
| Key: 1, Value: One    |
| Key: 2, Value: Two    |
|                       |
| Set:                  |
| - Red                 |
| - Green               |
| - Blue                |
-------------------------
Display a List of strings with three fruit names.
Display a Map with integer keys and string values.
Display a Set of strings with three color names.
Use Kotlin collection types: List, Map, Set.
Show each collection's items in a vertical Column.
Use Text composables for displaying items.
Starter Code
Android Kotlin
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable

@Composable
fun CollectionsDemo() {
    // TODO: Define List, Map, and Set collections
    // TODO: Display collections using Text inside Column
}
Task 1
Task 2
Task 3
Task 4
Task 5
Task 6
Solution
Android Kotlin
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable

@Composable
fun CollectionsDemo() {
    val fruits: List<String> = listOf("Apple", "Banana", "Cherry")
    val numbers: Map<Int, String> = mapOf(1 to "One", 2 to "Two")
    val colors: Set<String> = setOf("Red", "Green", "Blue")

    Column {
        Text(text = "List:")
        for (fruit in fruits) {
            Text(text = "- $fruit")
        }

        Text(text = "")
        Text(text = "Map:")
        for ((key, value) in numbers) {
            Text(text = "Key: $key, Value: $value")
        }

        Text(text = "")
        Text(text = "Set:")
        for (color in colors) {
            Text(text = "- $color")
        }
    }
}

This solution defines three Kotlin collections: a List of fruits, a Map of numbers, and a Set of colors.

It uses a Column composable to arrange the text vertically.

Each collection is displayed with a header and then its items are shown using Text composables in a simple loop.

This shows how to use Kotlin collections and display their contents in a Compose UI.

Final Result
Completed Screen
-------------------------
| Collections Demo       |
|-----------------------|
| List:                 |
| - Apple               |
| - Banana              |
| - Cherry              |
|                       |
| Map:                  |
| Key: 1, Value: One    |
| Key: 2, Value: Two    |
|                       |
| Set:                  |
| - Red                 |
| - Green               |
| - Blue                |
-------------------------
User sees the three collections displayed clearly.
No interactive elements; static display of collections.
Stretch Goal
Add a button that when clicked, adds a new fruit to the List and updates the UI.
💡 Hint
Use a mutableStateListOf for the List and remember { mutableStateListOf(...) } to hold state. Update the list on button click to trigger recomposition.

Practice

(1/5)
1. Which Kotlin collection type should you use to store a list of names where order matters and duplicates are allowed?
easy
A. List
B. Map
C. Set
D. Array

Solution

  1. Step 1: Understand collection types

    List keeps items in order and allows duplicates, Map stores key-value pairs, Set stores unique items without duplicates.
  2. Step 2: Match requirement to collection

    Since order matters and duplicates are allowed, List fits best.
  3. Final Answer:

    List -> Option A
  4. Quick Check:

    Order + duplicates = List [OK]
Hint: Remember: List = ordered + duplicates allowed [OK]
Common Mistakes:
  • Choosing Set which disallows duplicates
  • Choosing Map which stores key-value pairs
  • Confusing Array with List in Kotlin
2. Which of the following is the correct syntax to create a mutable map in Kotlin?
easy
A. val map = mutableListOf()
B. val map = mapOf()
C. val map = mutableMapOf()
D. val map = setOf()

Solution

  1. Step 1: Identify mutable map syntax

    mutableMapOf creates a mutable map; mapOf creates an immutable map.
  2. Step 2: Check other options

    mutableListOf creates a list, setOf creates a set; both are incorrect for map.
  3. Final Answer:

    val map = mutableMapOf() -> Option C
  4. Quick Check:

    Mutable map uses mutableMapOf [OK]
Hint: Mutable map uses mutableMapOf() function [OK]
Common Mistakes:
  • Using mapOf which is immutable
  • Using mutableListOf instead of mutableMapOf
  • Using setOf which creates a set, not a map
3. What will be the output of this Kotlin code?
val set = setOf(1, 2, 2, 3)
println(set.size)
medium
A. 3
B. 2
C. 4
D. Error

Solution

  1. Step 1: Understand set behavior

    Set stores unique elements only, so duplicates are removed.
  2. Step 2: Count unique elements

    Elements are 1, 2, 3 so size is 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Set removes duplicates, size = 3 [OK]
Hint: Set size counts unique elements only [OK]
Common Mistakes:
  • Counting duplicates in set size
  • Assuming set keeps duplicates
  • Expecting runtime error for duplicates
4. Identify the error in this Kotlin code snippet:
val map = mapOf("a" to 1, "b" to 2)
map["c"] = 3
medium
A. map["c"] = 3 is correct syntax
B. Syntax error in map declaration
C. Key "c" already exists
D. mapOf creates an immutable map, cannot add elements

Solution

  1. Step 1: Check mapOf mutability

    mapOf creates an immutable map, so you cannot add or change elements.
  2. Step 2: Analyze assignment

    map["c"] = 3 tries to add a new key-value pair, which is not allowed on immutable map.
  3. Final Answer:

    mapOf creates an immutable map, cannot add elements -> Option D
  4. Quick Check:

    mapOf = immutable map, no additions allowed [OK]
Hint: mapOf is immutable; use mutableMapOf to add items [OK]
Common Mistakes:
  • Assuming mapOf is mutable
  • Thinking syntax is wrong
  • Ignoring immutability of mapOf
5. Given a list of words, how can you create a map that counts how many times each word appears? Choose the correct Kotlin code snippet.
hard
A. val counts = words.filter { it.length > 1 }
B. val counts = words.groupingBy { it }.eachCount()
C. val counts = words.distinct()
D. val counts = words.toSet().map { it.length }

Solution

  1. Step 1: Understand the goal

    We want a map with words as keys and their counts as values.
  2. Step 2: Analyze options

    val counts = words.groupingBy { it }.eachCount() uses groupingBy and eachCount to count occurrences correctly. Others do not count frequencies.
  3. Final Answer:

    val counts = words.groupingBy { it }.eachCount() -> Option B
  4. Quick Check:

    groupingBy + eachCount counts items [OK]
Hint: Use groupingBy and eachCount to count list items [OK]
Common Mistakes:
  • Using toSet which removes duplicates but doesn't count
  • Using map to get lengths instead of counts
  • Filtering instead of counting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard