- Kotlin's Standard Library offers generic interfaces, classes, and functions for creating, populating, and managing collections.
- The supported collections can support one of the basic data structures of type List, Set or Map.
- List is an ordered collection elements which can be accessed by indices, and can occur more than once.
- Set is a collection of unique elements. Order of elements do not matter.
- Map (or dictionary) is a set of key-value pairs. Keys are unique, and each maps to exactly one value, which can have duplicates.
- Kotlin supported collections can be categorized as mutable or immutable types.
Mutable Collections
They supports both read and write functionalities i.e. mutable List, Set or 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
They support only read-only functionalities and can not be modified its elements i.e. immutable List, Set or 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)