Kotlin Standard Library: Complete Overview and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Kotlin Standard Library: Complete Overview



The Kotlin standard library (stdlib) is a powerful set of tools built directly into the language. It provides everything needed for everyday development: from working with collections and strings to coroutines and serialization. Unlike Java, Kotlin eliminates boilerplate code by offering a concise and expressive syntax. In this overview, we will break down the key stdlib modules with practical examples.



1. Working with Collections

The library offers immutable (List, Set, Map) and mutable (MutableList, MutableSet, MutableMap) collections with a rich set of higher-order functions.



val numbers = listOf(1, 2, 3, 4, 5)val doubled = numbers.map { it * 2 } // [2, 4, 6, 8, 10]val even = numbers.filter { it % 2 == 0 } // [2, 4]val sum = numbers.reduce { acc, i -> acc + i } // 15


Key functions:

  • map, filter, flatMap — transformation and filtering
  • groupBy, partition — grouping elements
  • zip, unzip — merging collections
  • chunked, windowed — splitting into parts


2. Strings and Regular Expressions

Kotlin extends String with methods absent in Java: removing prefixes/suffixes, checking for emptiness, templates.



val text = "  Hello, Kotlin!  "println(text.trim()) // "Hello, Kotlin!"println(text.removePrefix("  ")) // "Hello, Kotlin!  "println(text.contains(Regex("Kotlin"))) // true

// Multiline strings with trimIndentval json = """ { "name": "Kotlin", "year": 2016 }""".trimIndent()


3. Null Safety and Elvis Operator

One of the key features of Kotlin is built-in NPE protection. The standard library provides safe call functions (?.let) and the Elvis operator (?:).



val name: String? = getUserName()val length = name?.length ?: 0 // if name == null, then 0name?.let { println("Name is $it") } // executes only if name != null


4. Coroutines (kotlinx.coroutines)

Although coroutines are formally a separate library, they are considered part of the Kotlin ecosystem. They allow writing asynchronous code synchronously.



import kotlinx.coroutines.*

fun main() = runBlocking { launch { delay(1000L) println("World!") } println("Hello,")}


Main components:

  • launch — launching a coroutine without a return value
  • async — launching with a Deferred return
  • withContext — switching context (e.g., to the UI thread)
  • Channels (Channel) — for data transfer between coroutines


5. Serialization (kotlinx.serialization)

Built-in support for JSON, Protobuf, CBOR, and other formats via annotations.



import kotlinx.serialization.*import kotlinx.serialization.json.*

@Serializabledata class User(val name: String, val age: Int)

fun main() { val user = User("Alice", 30) val json = Json.encodeToString(user) println(json) // {"name":"Alice","age":30} val decoded = Json.decodeFromString<User>(json) println(decoded) // User(name=Alice, age=30)}


6. Streams and Sequences

For lazy evaluation, use Sequence instead of List — this saves memory when working with large data.



val seq = generateSequence(1) { it + 1 } // infinite sequence    .take(10)    .filter { it % 2 == 0 }    .toList()println(seq) // [2, 4, 6, 8, 10]


Extension functions

Recommendations