Collections
Collections
Unlike many languages, Kotlin distinguishes between mutable and immutable collections (lists, sets, maps, etc). Precise control over exactly when collections can be edited is useful for eliminating bugs, and for designing good APIs.
It is important to understand up front the difference between a read only view of a mutable collection, and an actually immutable collection. Both are easy to create, but the type system doesn't express the difference, so keeping track of that (if it's relevant) is up to you.
The Kotlin List<out T>
type is an interface that provides read only operations like size
, get
and so on. Like in Java, it inherits from Collection<T>
and that in turn inherits from Iterable<T>
. Methods that change the list are added by the MutableList<T>
interface. This pattern holds also for Set<out T>/Mutable