Using Kotlin's MutableList or ArrayList where lists are needed in Android

ArrayList is an implementation of the MutableList interface in Kotlin:

class ArrayList<E> : MutableList<E>, RandomAccess

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-list/index.html

That answer may indicate that MutableList should be chosen whenever possible, but ArrayList is a MutableList. So if you're already using ArrayList, there's really no reason to use MutableList instead, especially since you can't actually directly create an instance of it (MutableList is an interface, not a class).

In fact, if you look at the mutableListOf() Kotlin extension method:

public inline fun <T> mutableListOf(): MutableList<T> = ArrayList()

you can see that it just returns an ArrayList of the elements you supplied.


The difference is:

  • if you use ArrayList() you are explicitly saying "I want this to be an ArrayList implementation of MutableList and never change to anything else".

  • If you use mutableListOf() it is like saying "Give me the default MutableList implementation".

Current default implementation of the MutableList (mutableListOf()) returns an ArrayList. If in the (unlikely) event of this ever changing in the future (if a new more efficient implementation gets designed) this could change to ...mutableListOf(): MutableList<T> = SomeNewMoreEfficientList().

In that case, wherever in your code you used ArrayList() this will stay ArrayList. Wherever you have used mutableListOf() this would change from ArrayList to the brilliantly named SomeNewMoreEfficientList.