Collections help you store and organize multiple items together. They make it easy to work with groups of data in your app.
Collections (List, Map, Set) in Android Kotlin
val list: List<Type> = listOf(item1, item2, item3) val mutableList: MutableList<Type> = mutableListOf(item1, item2) val map: Map<KeyType, ValueType> = mapOf(key1 to value1, key2 to value2) val mutableMap: MutableMap<KeyType, ValueType> = mutableMapOf(key1 to value1) val set: Set<Type> = setOf(item1, item2) val mutableSet: MutableSet<Type> = mutableSetOf(item1)
List keeps items in order and can have duplicates.
Map stores key-value pairs for quick lookup by key.
Set stores unique items without duplicates.
Mutable collections let you add or remove items after creation.
val fruits: List<String> = listOf("Apple", "Banana", "Apple")
val phoneBook: Map<String, String> = mapOf("Alice" to "1234", "Bob" to "5678")
val uniqueTags: Set<String> = setOf("kotlin", "android", "kotlin")
val emptyList: List<Int> = listOf()
This program shows how to create and change a list, map, and set. It prints the collections before and after adding new items.
fun main() {
val shoppingList: MutableList<String> = mutableListOf("Milk", "Eggs")
println("Before adding: $shoppingList")
shoppingList.add("Bread")
println("After adding Bread: $shoppingList")
val contacts: MutableMap<String, String> = mutableMapOf("John" to "111-222")
println("Contacts before update: $contacts")
contacts["Mary"] = "333-444"
println("Contacts after adding Mary: $contacts")
val colors: MutableSet<String> = mutableSetOf("Red", "Green")
println("Colors before adding Red again: $colors")
colors.add("Red")
println("Colors after adding Red again: $colors")
}Lists keep order and allow duplicates; sets do not allow duplicates and have no order.
Maps let you find values fast by their keys.
Mutable collections let you change items; immutable ones do not.
Adding an existing item to a set does nothing because sets keep unique items.
Use List to keep ordered items, duplicates allowed.
Use Map to store key-value pairs for quick lookup.
Use Set to store unique items without duplicates.
Practice
Solution
Step 1: Understand collection types
List keeps items in order and allows duplicates, Map stores key-value pairs, Set stores unique items without duplicates.Step 2: Match requirement to collection
Since order matters and duplicates are allowed, List fits best.Final Answer:
List -> Option AQuick Check:
Order + duplicates = List [OK]
- Choosing Set which disallows duplicates
- Choosing Map which stores key-value pairs
- Confusing Array with List in Kotlin
Solution
Step 1: Identify mutable map syntax
mutableMapOf creates a mutable map; mapOf creates an immutable map.Step 2: Check other options
mutableListOf creates a list, setOf creates a set; both are incorrect for map.Final Answer:
val map = mutableMapOf() -> Option CQuick Check:
Mutable map uses mutableMapOf [OK]
- Using mapOf which is immutable
- Using mutableListOf instead of mutableMapOf
- Using setOf which creates a set, not a map
val set = setOf(1, 2, 2, 3) println(set.size)
Solution
Step 1: Understand set behavior
Set stores unique elements only, so duplicates are removed.Step 2: Count unique elements
Elements are 1, 2, 3 so size is 3.Final Answer:
3 -> Option AQuick Check:
Set removes duplicates, size = 3 [OK]
- Counting duplicates in set size
- Assuming set keeps duplicates
- Expecting runtime error for duplicates
val map = mapOf("a" to 1, "b" to 2)
map["c"] = 3Solution
Step 1: Check mapOf mutability
mapOf creates an immutable map, so you cannot add or change elements.Step 2: Analyze assignment
map["c"] = 3 tries to add a new key-value pair, which is not allowed on immutable map.Final Answer:
mapOf creates an immutable map, cannot add elements -> Option DQuick Check:
mapOf = immutable map, no additions allowed [OK]
- Assuming mapOf is mutable
- Thinking syntax is wrong
- Ignoring immutability of mapOf
Solution
Step 1: Understand the goal
We want a map with words as keys and their counts as values.Step 2: Analyze options
val counts = words.groupingBy { it }.eachCount() uses groupingBy and eachCount to count occurrences correctly. Others do not count frequencies.Final Answer:
val counts = words.groupingBy { it }.eachCount() -> Option BQuick Check:
groupingBy + eachCount counts items [OK]
- Using toSet which removes duplicates but doesn't count
- Using map to get lengths instead of counts
- Filtering instead of counting
Ready to prove it?
Pass the test and this pattern moves unknown
