0
0
Android Kotlinmobile~20 mins

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

Choose your learning style9 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.