11

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?

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

3 Comments

When is it recommended to use ArrayList over List?
@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
Yea, IMO mutableListOf is better overall than ArrayList
12

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.

6 Comments

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.
@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().
yes, that's right, of course. I forgot about that. So... trivially different, but indeed different. :-)
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
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>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.