Kotlin's Standard Library offers generic interfaces, classes, and functions for creating, populating, and managing collections efficiently and concisely. The supported collections are based on three basic data structures: List, Set, and Map.
Kotlin collections are categorized as mutable or immutable, based on whether their contents can be modified after creation.
Mutable Collections
Mutable collections support both read and write operations, allowing elements to be added, removed, or updated. Examples include mutable List, Set, and Map.val mutableList = mutableListOf("apple", "banana", "cherry") mutableList.add("date") mutableList.removeAt(1) mutableList[0] = "apricot" val mutableSet = mutableSetOf("apple", "banana", "cherry") mutableSet.add("date") mutableSet.remove("banana") val mutableMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3) mutableMap["four"] = 4 mutableMap.remove("two")
Immutable Collections
Immutable collections support only read operations, meaning their elements cannot be modified once the collection is created. Examples include immutable List, Set, and Map.val immutableList = listOf("apple", "banana", "cherry") val immutableSet = setOf("apple", "banana", "cherry") val immutableMap = mapOf("one" to 1, "two" to 2, "three" to 3)