androidengineers.Book a session

Programming Fundamentals & Kotlin

Collections and Standard Library

article30 minMedium

Collections store groups of values. Android apps use collections for feed items, messages, search results, cart products, lessons, users, and cached data.

Lists

A List keeps items in order.

val lessons = listOf("Kotlin", "Compose", "Room")
println(lessons[0])

Use listOf for read-only lists and mutableListOf when the list must change.

val completed = mutableListOf<String>()
completed.add("Kotlin")
completed.add("Compose")

Prefer immutable collections for UI state. Create a new list when state changes.

val updated = completed + "Room"

Sets

A Set stores unique values.

val selectedTags = setOf("Kotlin", "Android", "Kotlin")
println(selectedTags.size) // 2

Sets are useful when duplicates should not exist, such as selected filters.

Maps

A Map stores key-value pairs.

val scores = mapOf(
    "Kotlin" to 90,
    "Compose" to 85
)

println(scores["Kotlin"])

Maps are useful for lookups, caching, and grouping.

Standard Library Helpers

Kotlin gives expressive tools for transforming collections.

data class Lesson(val title: String, val completed: Boolean)

val lessons = listOf(
    Lesson("Kotlin", true),
    Lesson("Compose", false),
    Lesson("Room", false)
)

val remaining = lessons.filter { !it.completed }
val titles = lessons.map { it.title }
val allDone = lessons.all { it.completed }
val totalMinutes = lessons.sumOf { it.durationMinutes }

More helpers you will use constantly:

// Find a single item, or null if not found
val nextLesson = lessons.firstOrNull { !it.completed }

// Sort a list
val byTitle = lessons.sortedBy { it.title }
val byDuration = lessons.sortedByDescending { it.durationMinutes }

// Group items into a map
val byStatus = lessons.groupBy { it.completed }
// byStatus[true] -> completed lessons
// byStatus[false] -> incomplete lessons

Use these helpers instead of manual loops when they make intent clearer.

Practice

Create a list of lessons with title and duration. Print the total duration, the lessons longer than 40 minutes, and a list of just the titles.

Summary

Lists keep order, sets keep uniqueness, and maps connect keys to values. The Kotlin standard library — filter, map, firstOrNull, sortedBy, groupBy, sumOf — helps you transform data cleanly without writing manual loops. This is a daily skill in Android development.

YOUR LEARNING JOURNEY

0 of 22 available lessons completed

Progress saved in this browser. No account needed.
Collections and Standard Library | Junior Android Developer | Android Engineers