100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Collections in Kotlin

An overview of how Kotlin groups objects into Lists, Sets, and Maps using its dual mutable/read-only collection model.

CollectionsBeginner8 min readJul 8, 2026
Analogies

Introduction

A collection is a group of a variable number of items (possibly zero) that share significance and are processed together. Kotlin's standard library provides a rich set of tools for creating, populating, and managing collections of any type. Unlike Java, Kotlin makes a clear distinction at the interface level between read-only and mutable collections, which helps prevent accidental modification of data and encourages a more functional programming style.

🏏

Cricket analogy: A cricket squad list is a collection: a variable number of players sharing the purpose of representing the team, and Kotlin's read-only vs mutable split is like a public team-sheet you can view versus the selector's editable roster.

Syntax

kotlin
// Read-only collections
val readOnlyList: List<Int> = listOf(1, 2, 3)
val readOnlySet: Set<String> = setOf("a", "b")
val readOnlyMap: Map<String, Int> = mapOf("x" to 1)

// Mutable collections
val mutableList: MutableList<Int> = mutableListOf(1, 2, 3)
val mutableSet: MutableSet<String> = mutableSetOf("a", "b")
val mutableMap: MutableMap<String, Int> = mutableMapOf("x" to 1)

Explanation

Kotlin splits its collection interfaces into read-only (List, Set, Map) and mutable (MutableList, MutableSet, MutableMap) versions. The read-only interfaces expose only operations that access elements, while the mutable interfaces add operations like add(), remove(), and put(). It is important to understand that a read-only List is not necessarily immutable: it is simply a view that lacks mutating methods. If the same underlying collection is also referenced through a MutableList, changes made through that reference will be visible through the read-only view as well.

🏏

Cricket analogy: List/Set/Map for viewing versus MutableList/MutableSet/MutableMap for editing is like a fan's read-only scoreboard app versus the official scorer's editable console; if both point to the same live match data, a run the scorer adds instantly shows on the fan's view too.

Example

kotlin
fun main() {
    val numbers = mutableListOf(1, 2, 3)
    val readOnlyView: List<Int> = numbers // same underlying list

    numbers.add(4)
    println(readOnlyView) // reflects the change made via numbers

    val fruits = listOf("apple", "banana", "cherry")
    println("Total fruits: ${fruits.size}")
}
kotlin
[1, 2, 3, 4]
Total fruits: 3
  • Kotlin collections come in read-only (List, Set, Map) and mutable (MutableList, MutableSet, MutableMap) flavors.
  • Read-only means 'no mutating methods on this interface', not guaranteed deep immutability of the underlying data.
  • listOf(), setOf(), and mapOf() create read-only collections; mutableListOf(), mutableSetOf(), mutableMapOf() create mutable ones.
  • Choosing read-only interfaces by default improves code safety and predictability.
  • The three core collection types are List (ordered), Set (unique elements), and Map (key-value pairs).

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#CollectionsInKotlin#Collections#Syntax#Explanation#Example#StudyNotes#SkillVeris