Cheat Sheets

Kotlin Collections Cheat Sheet: List, Map, Filter, GroupBy & Sequences

By ArpaNeuro Team September 18, 2026
Kotlin collections cheat sheet

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: List has no add. Copy with list + item or toMutableList().
  • map / filter: Return new lists. mapNotNull drops nulls.
  • find: firstOrNull { }, singleOrNull when you expect 0–1.
  • groupBy: people.groupBy { it.city }Map<City, List<Person>>.
  • associate: associateBy { it.id } for id lookup maps.
  • flatten: listOfLists.flatten() or flatMap { 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 add on a List from listOf — it does not compile, then you force a mutable copy everywhere.
  • Using asSequence() on tiny lists “for performance” and making debugging harder.
  • !! after firstOrNull instead of ?: return.
  • Comparing lists with === when you meant structural ==.

FAQ

  1. List vs Array? List is the default. Array is a fixed-size JVM array, useful for varargs and some APIs. Prefer List in app code.
  1. Java collections from Kotlin? They come in as platform types (nullable unknown). Wrap at the boundary into Kotlin List/Map.
  1. Is BLOGPH0 a loop replacement? For side effects yes. For transforming, use map/filter so 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.

← All Articles Get a Quote →