Table of Contents
- What is Kotlin’s Standard Library?
- Core Concepts: Extension Functions & Properties
- Collections Framework
- Sequences: Lazy Evaluation for Large Data
- Ranges and Progressions
- String Manipulation
- I/O Operations
- Functional Programming Support
- Additional Utilities
- Conclusion
- References
1. What is Kotlin’s Standard Library?
The Kotlin Standard Library is a foundational set of APIs included with every Kotlin project, regardless of the target platform (JVM, JavaScript, or Native). It extends Java’s Standard Library (for JVM targets) with Kotlin-specific features while maintaining interoperability. The stdlib is designed to:
- Reduce boilerplate code.
- Enable concise, expressive syntax.
- Provide safe defaults (e.g., nullability checks).
- Support modern programming paradigms (e.g., functional programming).
Unlike Java, you don’t need to explicitly import most stdlib components—they’re available out of the box. For example, functions like listOf(), mapOf(), and println() are part of the stdlib.
2. Core Concepts: Extension Functions & Properties
One of Kotlin’s most powerful features is extension functions, which let you add methods to existing classes without inheritance or modifying the original class. The stdlib relies heavily on extensions to enhance Java classes (e.g., String, List) and Kotlin primitives.
Extension Functions
An extension function is defined with a receiver type (the class being extended) followed by a dot and the function name:
// Extension function to check if a string is a palindrome
fun String.isPalindrome(): Boolean {
val cleaned = this.lowercase().replace(Regex("[^a-z0-9]"), "")
return cleaned == cleaned.reversed()
}
// Usage
fun main() {
val text = "A man, a plan, a canal: Panama"
println(text.isPalindrome()) // Output: true
}
The stdlib includes hundreds of such extensions. For example:
String.isBlank(): Checks if the string is empty or contains only whitespace.List.firstOrNull(): Returns the first element ornullif the list is empty.Int.plus(other: Int): Adds two integers (yes, even arithmetic operations are extensions!).
Extension Properties
You can also extend classes with extension properties, which act like member properties but are defined outside the class:
// Extension property to get the number of words in a string
val String.wordCount: Int
get() = split(Regex("\\s+")).filter { it.isNotBlank() }.size
// Usage
fun main() {
val sentence = "Hello, Kotlin Standard Library!"
println(sentence.wordCount) // Output: 4
}
3. Collections Framework
Collections (lists, sets, maps, etc.) are central to most applications, and Kotlin’s stdlib elevates Java’s collections with a more intuitive, functional API.
3.1 Immutable vs. Mutable Collections
Kotlin distinguishes between immutable (read-only) and mutable (read-write) collections, encouraging immutability by default:
| Immutable (Read-Only) | Mutable (Read-Write) |
|---|---|
listOf() | mutableListOf() |
setOf() | mutableSetOf() |
mapOf() | mutableMapOf() |
Immutable collections guarantee thread safety and prevent accidental modifications, while mutable collections allow in-place changes:
// Immutable list (cannot add/remove elements)
val immutableList = listOf(1, 2, 3)
// immutableList.add(4) // Compile error!
// Mutable list (can modify)
val mutableList = mutableListOf(1, 2, 3)
mutableList.add(4)
println(mutableList) // Output: [1, 2, 3, 4]
3.2 Common Collection Interfaces
Kotlin’s collection hierarchy mirrors Java’s but with clearer separation between immutable and mutable types:
List: Ordered collection with duplicate elements (e.g.,[1, 2, 2, 3]).Set: Unordered collection with unique elements (e.g.,{1, 2, 3}).Map: Key-value pairs with unique keys (e.g.,{name: "Alice", age: 30}).
Example:
val fruits = listOf("apple", "banana", "cherry") // List
val uniqueNumbers = setOf(1, 2, 2, 3) // Set (duplicates removed)
val person = mapOf("name" to "Bob", "age" to 25) // Map (uses `to` for key-value pairs)
3.3 Essential Collection Utility Functions
The stdlib provides dozens of utility functions to transform, filter, and analyze collections. Here are the most常用的:
filter(predicate)
Returns elements that match a condition:
val numbers = listOf(1, 2, 3, 4, 5)
val evenNumbers = numbers.filter { it % 2 == 0 } // [2, 4]
map(transform)
Applies a transformation to each element:
val squared = numbers.map { it * it } // [1, 4, 9, 16, 25]
groupBy(keySelector)
Groups elements by a key:
val words = listOf("apple", "banana", "apricot", "cherry")
val groupedByFirstLetter = words.groupBy { it.first() }
// Output: {a=[apple, apricot], b=[banana], c=[cherry]}
fold(initial, operation)
Accumulates elements into a single result using an initial value:
val sum = numbers.fold(0) { acc, num -> acc + num } // 0 + 1 + 2 + 3 + 4 + 5 = 15
firstOrNull(predicate) / lastOrNull(predicate)
Returns the first/last element matching a condition, or null if none:
val firstEven = numbers.firstOrNull { it % 2 == 0 } // 2
val lastOdd = numbers.lastOrNull { it % 2 != 0 } // 5
4. Sequences: Lazy Evaluation for Large Data
While collections process data eagerly (all elements at once), sequences (Sequence<T>) process elements lazily—only when needed. This makes them ideal for:
- Large datasets (e.g., reading from a file or database).
- Infinite data streams.
- Avoiding intermediate collections (e.g., chaining
filterandmap).
How Sequences Work
A sequence pipeline has two types of operations:
- Intermediate:
filter,map,sorted(return a new sequence, no execution). - Terminal:
toList,count,sum(trigger execution and return a result).
Example: Eager vs. Lazy Processing
// Eager: Creates intermediate collections (list → filtered list → mapped list)
val eagerResult = listOf(1, 2, 3, 4, 5)
.filter { it % 2 == 0 } // [2, 4]
.map { it * 2 } // [4, 8]
.first() // 4 (but processes all elements)
// Lazy: Processes elements one at a time until the terminal operation
val lazyResult = listOf(1, 2, 3, 4, 5).asSequence()
.filter { it % 2 == 0 } // No execution yet
.map { it * 2 } // No execution yet
.first() // 4 (stops after finding the first match: 2 → 4)
Use asSequence() to convert a collection to a sequence.
5. Ranges and Progressions
Ranges represent intervals of values (e.g., 1 to 10), and progressions are ordered sequences of values derived from ranges. They simplify iteration and condition checks.
Basic Ranges
IntRange:1..10(includes 1 and 10).CharRange:'a'..'z'(all lowercase letters).- Exclusive Range:
1 until 10(includes 1-9).
val oneToFive = 1..5
val aToE = 'a'..'e'
val oneUntilTen = 1 until 10 // 1-9
// Check if a value is in a range
println(3 in oneToFive) // true
println('f' in aToE) // false
Progressions
Progressions add step logic to ranges:
downTo: Reverse order (e.g.,5 downTo 1).step: Specify increment/decrement (e.g.,1..10 step 2).
// Countdown from 5 to 1
for (i in 5 downTo 1) print("$i ") // Output: 5 4 3 2 1
// Even numbers from 2 to 10
for (i in 2..10 step 2) print("$i ") // Output: 2 4 6 8 10
6. String Manipulation
Kotlin’s String extensions simplify common text operations, eliminating verbose Java code (e.g., StringUtils from Apache Commons).
Key String Functions
trim()/trimIndent(): Remove whitespace.trimIndent()cleans up multiline strings.split(delimiters): Split into a list using delimiters (supports regex).take(n)/drop(n): Take/drop the firstncharacters.replace(old, new)/replace(regex, replacement): Replace substrings or regex matches.startsWith(prefix)/endsWith(suffix): Check prefixes/suffixes.
Examples:
val text = " Hello, Kotlin! "
// Trim whitespace
println(text.trim()) // "Hello, Kotlin!"
// Split with regex (split on commas or spaces)
val parts = "apple, banana; cherry".split(Regex("[,;\\s]+"))
// ["apple", "banana", "cherry"]
// Take first 5 characters
println("Kotlin".take(3)) // "Kot"
// Replace regex (remove digits)
val cleaned = "abc123def456".replace(Regex("\\d"), "") // "abcdef"
// Multiline string with trimIndent()
val poem = """
Roses are red,
Violets are blue,
Kotlin is neat,
And so are you!
""".trimIndent()
// Output (no leading spaces):
// Roses are red,
// Violets are blue,
// Kotlin is neat,
// And so are you!
7. I/O Operations
Kotlin simplifies file handling with extensions on Java’s java.io.File and java.nio.file.Path classes.
Reading/Writing Files
File.readText(): Reads the entire file as a string.File.writeText(text): Writes text to a file (overwrites existing content).File.appendText(text): Appends text to a file.File.readLines(): Reads all lines into aList<String>.
Example:
import java.io.File
fun main() {
val file = File("example.txt")
// Write to file
file.writeText("Hello, Kotlin Stdlib!")
// Read from file
val content = file.readText()
println(content) // "Hello, Kotlin Stdlib!"
// Append to file
file.appendText("\nAdding a new line!")
// Read lines
val lines = file.readLines()
println(lines) // ["Hello, Kotlin Stdlib!", "Adding a new line!"]
}
8. Functional Programming Support
Kotlin embraces functional programming with:
- Lambdas: Anonymous functions (e.g.,
{ x: Int -> x * 2 }). - Higher-order functions: Functions that take/return other functions (e.g.,
filter,map). - Lambda with receiver: Functions like
applyorwiththat let you call methods on a receiver object.
Lambda with Receiver: apply and with
These functions simplify configuring objects by eliminating repetitive receiver references:
// Without apply: repetitive "person."
val person = Person()
person.name = "Alice"
person.age = 30
person.email = "[email protected]"
// With apply: configures the object and returns it
val person = Person().apply {
name = "Alice"
age = 30
email = "[email protected]"
}
with(receiver) { ... } is similar but returns the last expression instead of the receiver:
val personInfo = with(person) {
"Name: $name, Age: $age" // Returns this string
}
9. Additional Utilities
The stdlib includes handy utilities for common tasks:
Null Safety Helpers
checkNotNull(value): ThrowsIllegalStateExceptionifvalueisnull.require(condition): ThrowsIllegalArgumentExceptionifconditionisfalse.elvis operator (?:): Returns a default value ifnull(e.g.,name ?: "Guest").
TODO()
Marks incomplete code and throws NotImplementedError at runtime:
fun calculateTotal(): Double {
TODO("Implement this method later") // Throws error when called
}
Pair and Triple
Simple data holders for 2 or 3 values (avoid creating custom classes for trivial cases):
val coordinates = Pair(10.0, 20.0) // (first=10.0, second=20.0)
val user = Triple("Alice", 30, "[email protected]") // (first=Alice, second=30, [email protected])
10. Conclusion
Kotlin’s Standard Library is the backbone of Kotlin development, offering tools to write concise, efficient, and safe code. From collections and sequences to extension functions and ranges, the stdlib eliminates boilerplate and empowers developers to focus on logic rather than mechanics.
To master the stdlib, explore the official documentation and experiment with its functions in your projects. The more you use it, the more you’ll appreciate how it transforms your development workflow.