The Kotlin Standard Library provides a comprehensive set of tools for managing collections – groups of a variable number of items (possibly zero) that are significant to the problem being solved and are commonly operated on.
Collections are a common concept for most programming languages, so if you're familiar with, for example, Java or Python collections, you can skip this introduction and proceed to the detailed sections.
A collection usually contains a number of objects of the same type (and its subtypes). Objects in a collection are called elements or items. For example, all the students in a department form a collection that can be used to calculate their average age.
The following collection types are relevant for Kotlin:
- List is an ordered collection with access to elements by indices – integer numbers that reflect their position. Elements can occur more than once in a list. An example of a list is a telephone number: it's a group of digits, their order is important, and they can repeat.
- Set is a collection of unique elements. It reflects the mathematical abstraction of set: a group of objects without repetitions. Generally, the order of set elements has no significance. For example, the numbers on lottery tickets form a set: they are unique, and their order is not important.
- Map (or dictionary) is a set of key-value pairs. Keys are unique, and each of them maps to exactly one value. The values can be duplicates. Maps are useful for storing logical connections between objects, for example, an employee's ID and their position.
Kotlin lets you manipulate collections independently of the exact type of objects stored in them. In other words, you add
a String
to a list of String
s the same way as you would do with Int
s or a user-defined class.
So, the Kotlin Standard Library offers generic interfaces, classes, and functions for creating, populating, and managing
collections of any type.
The collection interfaces and related functions are located in the kotlin.collections
package. Let's get an overview
of its contents.
Arrays are not a type of collection. For more information, see Arrays.
{type="note"}
The Kotlin Standard Library provides implementations for basic collection types: sets, lists, and maps. A pair of interfaces represent each collection type:
- A read-only interface that provides operations for accessing collection elements.
- A mutable interface that extends the corresponding read-only interface with write operations: adding, removing, and updating its elements.
Note that a mutable collection doesn't have to be assigned to a var
. Write operations with
a mutable collection are still possible even if it is assigned to a val
. The benefit of assigning mutable collections to
val
is that you protect the reference to the mutable collection from modification. Over time, as your code grows and becomes
more complex, it becomes even more important to prevent unintentional modification to references. Use val
as much as possible
for safer and more robust code. If you try to reassign a val
collection, you get a compilation error:
fun main() {
//sampleStart
val numbers = mutableListOf("one", "two", "three", "four")
numbers.add("five") // this is OK
println(numbers)
//numbers = mutableListOf("six", "seven") // compilation error
//sampleEnd
}
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
The read-only collection types are covariant.
This means that, if a Rectangle
class inherits from Shape
, you can use a List<Rectangle>
anywhere the List<Shape>
is required.
In other words, the collection types have the same subtyping relationship as the element types. Maps are covariant on
the value type, but not on the key type.
In turn, mutable collections aren't covariant; otherwise, this would lead to runtime failures. If MutableList<Rectangle>
was a subtype of MutableList<Shape>
, you could insert other Shape
inheritors (for example, Circle
) into it, thus
violating its Rectangle
type argument.
Below is a diagram of the Kotlin collection interfaces:
Let's walk through the interfaces and their implementations. To learn about Collection
, read the section below.
To learn about List
, Set
, and Map
, you can either read the corresponding sections or watch a video
by Sebastian Aigner, Kotlin Developer Advocate: