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

13,660

Solution 1

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.

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.

Share:
13,660
Mike Williamson
Author by

Mike Williamson

Slowly moving away from doing the fun technical work. :(

Updated on August 05, 2022

Comments

  • Mike Williamson
    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 versus MutableList.

    It seems to me that MutableList should be chosen whenever possible. Yet, if I look at Android examples, they seem to always choose the ArrayList (as far as I've found so far).

    Below is a snippet of a working example that uses ArrayList and extends Java's RecyclerView.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 of ArrayList<>), 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 over ArrayList? What are the main reasons? Some of that link I provide above goes over my head, but it seems to me that MutableList is a looser implementation that is more capable of changing and improving in the future. Is that right?

  • Mike Williamson
    Mike Williamson over 5 years
    Yes, @TheWanderer, I saw that mutableListOf is really just ArrayList under the hood. I somehow didn't notice that MutableList was just an interface. So, this helps! But why even have mutableListOf 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
    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
    Mike Williamson over 5 years
    yes, that's right, of course. I forgot about that. So... trivially different, but indeed different. :-)
  • IgorGanapolsky
    IgorGanapolsky over 4 years
    When is it recommended to use ArrayList over List?
  • Vlad
    Vlad about 4 years
    @IgorGanapolsky you would use ArrayList explicitly If you wanted to ensure that your implementation would never change to anything else (stay ArrayList event if mutableListOf() changes in future to return something else). Note that, probably the only reason an implementation would change would be the advent of some new SomeNewMoreEfficientList to replace the ArrayList. 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 use mutableListOf
  • IgorGanapolsky
    IgorGanapolsky about 4 years
    Yea, IMO mutableListOf is better overall than ArrayList
  • Mihae Kheel
    Mihae Kheel about 3 years
    Since 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
    Mihae Kheel about 3 years
    Why 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
    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/…