Lists

Learn how to create and use lists in Kotlin to store and manipulate multiple values in one data structure.

Collections are a central feature of any programming language. They allow storing multiple values – such as your list of favorite books – in one convenient data structure which can then be stored and accessed with a single variable.

The fundamental collection types you’ll master in the following lessons are:

  • Lists
  • Sets
  • Arrays*
  • Maps

*Arrays are not part of the actual Collections API but are included here because they’re also a data structure for storing multiple elements.

Creating a List #

Kotlin provides three convenient functions to initialize lists:

Press + to interact
val siUnits = listOf("s", "m", "kg", "A", "K", "mol", "cd") // Creates readonly list
val quarks = mutableListOf("up", "down", "charm", "strange", "top", "bottom") // Creates mutable list
val physicists = arrayListOf("Albert Einstein", "Isaac Newton") // Creates mutable list

All three functions allow you to add any number of elements (try it out!). The main difference between them is that listOf creates a read-only list whereas mutableListOf creates a mutable list. What does this mean? With read-only lists, you cannot add or remove elements from the list after it’s created. With a mutable list, ...

Access this course and 1400+ top-rated courses and projects.