Skip to content
DeveloperMemos

The Difference Between List and MutableList in Kotlin

Kotlin, Lists, Mutable Lists2 min read

When working with collections in Kotlin, it is important to understand the distinction between List and MutableList. Both types are used to store multiple elements, but they differ in terms of immutability. In this article, we will delve into the characteristics and usage scenarios of these two types to help you make informed decisions when working with collections in your Android development projects.

List

The List interface represents an immutable collection of elements in Kotlin. Once initialized, a List cannot be modified by adding or removing elements. It provides read-only access to its elements and supports operations like accessing elements by index, checking for element existence, and iterating over the collection.

Here's an example of creating and using a List in Kotlin:

1val fruits = listOf("Apple", "Orange", "Banana")
2println(fruits[0]) // Output: Apple
3println(fruits.contains("Orange")) // Output: true
4for (fruit in fruits) {
5 println(fruit)
6}

In this example, we create a List of fruits and perform various operations such as accessing an element by index (fruits[0]), checking if the list contains a specific element (fruits.contains("Orange")), and iterating over the elements using a loop.

It's important to note that since List is immutable, attempting to add or remove elements will result in a compilation error.

MutableList

On the other hand, the MutableList interface represents a modifiable collection of elements. Unlike List, a MutableList allows adding, removing, and modifying elements after initialization. It provides both read and write access to its elements and supports operations such as adding elements, removing elements, updating elements at a specific index, and more.

Let's take a look at an example of using a MutableList in Kotlin:

1val numbers = mutableListOf(1, 2, 3)
2numbers.add(4)
3numbers.remove(2)
4numbers[0] = 5
5
6println(numbers) // Output: [5, 3, 4]

In this example, we create a MutableList of numbers and perform various operations such as adding an element (numbers.add(4)), removing an element (numbers.remove(2)), and updating an element at index 0 (numbers[0] = 5).

As you can see, MutableList provides the flexibility to modify the collection based on your application's requirements.

When to Use List vs. MutableList

The decision to use either List or MutableList depends on whether you need an immutable or mutable collection in your codebase.

Use List when:

  • The collection should remain unchanged after initialization.
  • You only need to read the elements and don't require modification operations.
  • Immutability is preferred for functional programming style or data integrity.

Use MutableList when:

  • The collection needs to be modified dynamically.
  • You want to add, remove, or update elements during the course of your program.
  • Flexibility and mutability are essential for your use case.

It's worth mentioning that if you receive a List from an external source but need to modify it, you can convert it to a MutableList using the toMutableList() function.

1val immutableList = listOf(1, 2, 3)
2val mutableList = immutableList.toMutableList()
3mutableList.add(4)
4
5println(mutableList) // Output: [1, 2, 3, 4]

In Closing

In summary, understanding the difference between List and MutableList is crucial when working with collections in Kotlin. While List provides an immutable collection that cannot be modified after initialization, MutableList offers flexibility by allowing addition, removal, and modification of elements. Choosing the appropriate type ensures your code aligns with your specific requirements and helps maintain clarity and correctness in your Android applications.