Kotlin collections are read-only by default (List, Map, Set) with mutable variants when you need to edit in place. map, filter, groupBy, and firstOrNull replace most Java loops. Operations are eager unless you switch to asSequence() for long chains on large lists.
This Kotlin cheat sheet is written for people searching for a fast, accurate reference: students, junior developers, and teams shipping production software. Pin it, copy the snippets, and come back when syntax slips.
Quick reference
- Create:
listOf,mutableListOf,mapOf,buildList { add(1) }. - Read-only:
Listhas noadd. Copy withlist + itemortoMutableList(). - map / filter: Return new lists.
mapNotNulldrops nulls. - find:
firstOrNull { },singleOrNullwhen you expect 0–1. - groupBy:
people.groupBy { it.city }→Map<City, List<Person>>. - associate:
associateBy { it.id }for id lookup maps. - flatten:
listOfLists.flatten()orflatMap { it.items }. - Sequence:
asSequence().map{}.filter{}.toList()— lazy, one pass when terminated.
Copy-paste examples
Filter, sort, and group products
Keep domain data classes small. sortedBy does not mutate the source list.
data class Product(val id: Int, val title: String, val price: Int, val active: Boolean)
fun catalog(items: List<Product>): Map<Boolean, List<String>> =
items.filter { it.active }
.sortedBy { it.price }
.groupBy { it.price < 50 }
.mapValues { (_, rows) -> rows.map { it.title } }
fun byId(items: List<Product>): Map<Int, Product> =
items.associateBy { it.id }Common mistakes
- Calling
addon aListfromlistOf— it does not compile, then you force a mutable copy everywhere. - Using
asSequence()on tiny lists “for performance” and making debugging harder. !!afterfirstOrNullinstead of?: return.- Comparing lists with
===when you meant structural==.
FAQ
- List vs Array?
Listis the default.Arrayis a fixed-size JVM array, useful for varargs and some APIs. PreferListin app code.
- Java collections from Kotlin? They come in as platform types (nullable unknown). Wrap at the boundary into Kotlin
List/Map.
- Is BLOGPH0 a loop replacement? For side effects yes. For transforming, use
map/filterso the intent is obvious.
Related Kotlin cheat sheets
Build with this stack
When a cheat sheet is not enough — you need a production app, a student FYP, or a custom dashboard — ArpaNeuro builds mobile app development and also sells ready-made source code. Request a quote and tell us the stack.
Browse software development services or the source code marketplace if you want a working codebase instead of starting from a blank file.