Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 803
Mike Williamson Avatar asked Nov 09 '18 01:11

Mike Williamson


People also ask

What is the difference between mutable list and ArrayList Kotlin?

The only difference between the two is communicating your intent. When you write val a = mutableListOf() , you're saying "I want a mutable list, and I don't particularly care about the implementation". When you write, instead, val a = ArrayList() , you're saying "I specifically want an ArrayList ".

Is Kotlin list an ArrayList?

ArrayList class is used to create a dynamic array in Kotlin. Dynamic array states that we can increase or decrease the size of an array as per requisites. It also provide read and write functionalities.

How to create a mutable ArrayList in Kotlin?

We can create an ArrayList implementation of the mutable list simply by using a constructor: This is Kotlin’s implementation of dynamic arrays. We can also create a mutable list by using a dedicated method:

What is the difference between array and list in Kotlin?

Moreover Array is mutable whereas List is not. Furthermore kotlin.collections.List is an interface implemented among others by java.util.ArrayList. It's also extended by kotlin.collections.MutableList to be used when a collection that allows for item modification is needed.

How do you create a list in Kotlin?

Creating Kotlin Lists For list creation, use the standard library functions listOf () for read-only lists and mutableListOf () for mutable lists. To prevent unwanted modifications, obtain read-only views of mutable lists by casting them to List.

What are the different collection types in Kotlin?

There are different basic collection types in Kotlin: lists, sets, and maps. This codelab focused specifically on lists, and you'll learn more about sets and maps in future codelabs.


Video Answer


2 Answers

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.

like image 181
Vlad Avatar answered Oct 18 '22 05:10

Vlad


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.

like image 31
TheWanderer Avatar answered Oct 18 '22 07:10

TheWanderer