Kotlin Standard Library: Complete Overview

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 that comes with the compiler and provides basic functionality for everyday development. Unlike Java, where many operations require external libraries, Kotlin offers built-in solutions for working with collections, strings, coroutines, serialization, and file input/output. In this article, we will thoroughly examine the key modules and functions of the Kotlin Standard Library with practical examples.



1. Basic Types and Their Capabilities



The library includes extended functions for primitive types and strings:



  • Numeric types — Int, Double, Long and their extensions (.toInt(), .coerceIn()).
  • Strings — templates ("Hello, $name"), multiline strings ("""..."""), methods replace(), split(), trimIndent().
  • Ranges — 1..10, step 2, downTo.


// Example of working with strings and rangesfun main() {    val name = "Kotlin"    val greeting = "Hello, $name!"    println(greeting) // Hello, Kotlin!

for (i in 1..5 step 2) { println(i) // 1, 3, 5 }}


2. Collections: List, Set, Map



Kotlin separates collections into mutable (MutableList, MutableSet, MutableMap) and immutable (List, Set, Map). Main capabilities:



  • Creation — listOf(), setOf(), mapOf().
  • Filtering and transformation — filter(), map(), flatMap(), groupBy().
  • Aggregation — sum(), count(), reduce(), fold().
  • Sorting — sorted(), sortedBy().


// Example of working with collectionsfun main() {    val numbers = listOf(1, 2, 3, 4, 5)    val evenSquares = numbers        .filter { it % 2 == 0 }        .map { it * it }    println(evenSquares) // [4, 16]

val grouped = numbers.groupBy { if (it % 2 == 0) "even" else "odd" } println(grouped) // {odd=[1, 3, 5], even=[2, 4]}}


3. Sequences



For lazy evaluation, Kotlin offers Sequence. This is especially useful when working with large volumes of data:



fun main() {    val result = (1..1_000_000)        .asSequence()        .filter { it % 2 == 0 }        .map { it * 2 }        .take(5)        .toList()    println(result) // [4, 8, 12, 16, 20]}


4. Null-safety and Elvis Operator



One of the key features of Kotlin is safe null handling. The library provides:



  • Elvis operator (?:) — sets a default value.
  • Safe call (?.) — prevents NullPointerException.
  • let function — executes code only if the object is not null.


fun main() {    val name: String? = null    val length = name?.length ?: 0    println(length) // 0

name?.let { println("Name is not null: $it") }}


5. Working with Files and Streams



The standard library includes convenient extensions for java.io.File:



  • readText(), writeText() — reading/writing strings.
  • readLines() — reading line by line.
  • useLines() — safe work with streams.
  • copyTo(),

Recommendations