Using Kotlin's MutableList or ArrayList where lists are needed in Android
Solution 1
The difference is:
if you use
ArrayList()
you are explicitly saying "I want this to be anArrayList
implementation ofMutableList
and never change to anything else".If you use
mutableListOf()
it is like saying "Give me the defaultMutableList
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
.
Solution 2
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.
Mike Williamson
Slowly moving away from doing the fun technical work. :(
Updated on August 05, 2022Comments
-
Mike Williamson over 1 year
I am trying to learn the "Kotlin native way" to do things in Android, while being an expert in neither Kotlin, Java, nor Android development. Specifically, when to use
ArrayList
versusMutableList
.It seems to me that
MutableList
should be chosen whenever possible. Yet, if I look at Android examples, they seem to always choose theArrayList
(as far as I've found so far).Below is a snippet of a working example that uses
ArrayList
and extends Java'sRecyclerView.Adapter
.class PersonListAdapter(private val list: ArrayList<Person>, private val context: Context) : RecyclerView.Adapter<PersonListAdapter.ViewHolder>() {
Question 1)
Could I simply write the above code as follows (note
MutableList<>
instead ofArrayList<>
), even though I am borrowing from Android's Java code?class PersonListAdapter(private val list: MutableList<Person>, private val context: Context) : RecyclerView.Adapter<PersonListAdapter.ViewHolder>() {
Question 2)
Is it really better to always use
MutableList
overArrayList
? What are the main reasons? Some of that link I provide above goes over my head, but it seems to me thatMutableList
is a looser implementation that is more capable of changing and improving in the future. Is that right? -
Mike Williamson over 5 yearsYes, @TheWanderer, I saw that
mutableListOf
is really justArrayList
under the hood. I somehow didn't notice thatMutableList
was just an interface. So, this helps! But why even havemutableListOf
in this case? I guess I still don't understand the philosophy; to me, it seems a bit like polluting a namespace: there are now two functions that do precisely the same thing. To me, it seems like bad programming to increase cognitive load like this with no benefit whatsoever, but I figured I must be wrong and I had to be missing something. -
TheWanderer over 5 years@MikeWilliamson it returns an instance of ArrayList, but under the MutableList type. I don't know any reason you'd want to use that, but it is different from
arrayListOf()
. -
Mike Williamson over 5 yearsyes, that's right, of course. I forgot about that. So... trivially different, but indeed different. :-)
-
IgorGanapolsky over 4 yearsWhen is it recommended to use
ArrayList
overList
? -
Vlad about 4 years@IgorGanapolsky you would use
ArrayList
explicitly If you wanted to ensure that your implementation would never change to anything else (stayArrayList
event ifmutableListOf()
changes in future to return something else). Note that, probably the only reason an implementation would change would be the advent of some newSomeNewMoreEfficientList
to replace theArrayList
. From that perspective one would probably want the swap to happen. Unless you identify something specific from ArrayList your app depends on and you believe it is something which might change, then usemutableListOf
-
IgorGanapolsky about 4 yearsYea, IMO
mutableListOf
is better overall than ArrayList -
Mihae Kheel about 3 yearsSince
ArrayList is an implementation of the MutableList interface
I don't get the reason why when working with Moshi and Retrofit2 with RxJava you cannot use Obeservable<ArrayList<YourClass>> only MutableList, List and other types stated here github.com/square/moshi#built-in-type-adapters -
Mihae Kheel about 3 yearsWhy when I go to the source code of ArrayList in Kotlin it does not show that it was derived from MutableList? It redirect me to TypeAliases.ktx with this line of code
@SinceKotlin("1.1") public actual typealias ArrayList<E> = java.util.ArrayList<E>
-
TheWanderer about 3 years@MihaeKheel I don't know how those libraries work internally, and interoperability can be complex, but Kotlin's ArrayList both implements MutableList and is a typealias for Java's ArrayList. kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/…