Differences between arrayListOf, listOf and mutableListOf in Kotlin

Learn via video courses
Topics Covered

Overview

In the world of Kotlin, a popular statically typed programming language, lists play a crucial role in handling and manipulating a group of related objects. Among these collections, arrayListOf(), listOf(), and mutableListOf() are commonly used functions, each with its unique characteristics and use cases. In this article, we will go through the differences between these three functions and provide a complete comparison to help developers make informed decisions when working with lists in Kotlin.

Difference Between arrayListOf() and listOf()

The key differences between the arrayListOf() and listOf() function in Kotlin are as follows:

FeaturearrayListOf()listOf()
TypeReturns a Mutable List.Returns a read-only List.
MutabilityMutable - Allows modification (add, remove) of elements after creation.Immutable - Elements cannot be added or removed after creation.
UsagePreferred when you need to modify the list (add/remove elements) after creation.Preferred when the list remains static (immutable) after creation.
ConstructorRequires the ArrayList() constructor.Requires the listOf() constructor.
SyntaxarrayListOf(element1, element2, ...) or arrayListOf() for an empty list.listOf(element1, element2, ...) or listOf() for an empty list.
Importimport java.util.ArrayListNo specific import is needed as it is a part of the Kotlin standard library.
PerformanceLess efficient due to its mutability.More efficient due to its immutability. Also, optimized for read-only operations.

Difference between listOf() and mutableListOf()

The key differences between the listOf() and mutableListOf() function in Kotlin are as follows:

FeaturelistOf()mutableListOf()
MutabilityImmutable - Elements cannot be added or removed after creation.Mutable - Allows modification (add, remove) of elements after creation.
UsageUse listOf() when you want a list of items that do not change over time.Use mutableListOf() when you need a list that can be modified during the execution of your program.
ConstructorRequires the listOf() constructor.Requires the mutableListOf() constructor.
SyntaxlistOf(element1, element2, ...) or listOf() for an empty list.mutableListOf(element1, element2, ...) or mutableListOf() for an empty list.
MethodsSupports read-only access methods like get(), indexOf(), subList(), contains(), size(), etc.Supports all methods of listOf() and additional methods for modifying the list like add(), addAll(), removeAt(), etc.
PerformanceMore efficient due to its immutability. Also, optimized for read-only operations.Less efficient due to its mutability.

Difference between arrayListOf() and mutableListOf()

The key differences between the arrayListOf() and mutableListOf() function in Kotlin are as follows:

FeaturearrayListOf()mutableListOf()
DefinitionarrayListOf() is a function that returns an instance of ArrayList, which is a concrete implementation of the MutableList interface.mutableListOf() is a function that returns a MutableList. The actual implementation is abstracted away.
IntentionWhen you use arrayListOf(), you’re specifically asking for an ArrayList.When you use mutableListOf(), you’re asking for a mutable list and don’t particularly care about the implementation.
ImplementationarrayListOf() returns the ArrayList as an actual ArrayList.mutableListOf() returns a MutableList, so the actual ArrayList is “disguised” as just the parts that are described by the MutableList interface.
SyntaxarrayListOf(element1, element2, ...) or arrayListOf() for an empty list.mutableListOf(element1, element2, ...) or mutableListOf() for an empty list.

Comprehensive Comparison: arrayListOf(), listOf(), and mutableListOf()

Here's a comprehensive comparison between arrayListOf(), listOf(), and mutableListOf() in Kotlin:

FeaturearrayListOf()listOf()mutableListOf()
DefinitionarrayListOf() is a function that returns an instance of ArrayList, which is a concrete implementation of the MutableList interface.listOf() is a function that returns an instance of List, which is an interface for a read-only (immutable) list.mutableListOf() is a function that returns a MutableList. The actual implementation is abstracted away.
Exampleval arrayList = ArrayList\<String>() arrayList.add("Kotlin") arrayList.add("Java")val list = listOf\<String>("Kotlin", "Java")val mutableList = mutableListOf\<String>() mutableList.add("Kotlin") mutableList.add("Java")
IntentionWhen you use arrayListOf(), you’re specifically asking for an ArrayList.When you use listOf(), you’re asking for an immutable list.When you use mutableListOf(), you’re asking for a mutable list and don’t particularly care about the implementation.
ImplementationarrayListOf() returns the ArrayList as an actual ArrayList.listOf() returns a List, so the actual ArrayList is “disguised” as just the parts that are described by the List interface.mutableListOf() returns a MutableList, so the actual ArrayList is “disguised” as just the parts that are described by the MutableList interface.

Conclusion

  1. While working with a list in Kotlin, you have three main functions: arrayListOf(), listOf(), and mutableListOf(). Each of these has its unique purpose and characteristics.
  2. The arrayListOf() or mutableListOf() functions can be used to create a dynamic list that can be updated over time by adding or removing elements. These are known as mutable lists. However, it is important to note that these lists are slower because they have to update their size every time an element gets added or removed.
  3. On the other hand, if you don’t need to change the list after creating it, you can use the listOf() function. This creates an ‘immutable’ list that can’t be changed, but it’s faster because it doesn’t need to resize.
  4. Therefore, when working with lists in Kotlin, it’s essential to choose the function that best suits your needs based on these differences. Understanding these differences will enable you to write more efficient and effective Kotlin code.