Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
Difference between ArrayList<String>() and mutableListOf<String>() in Kotlin
private val repositories = mutableListOf<String>()

private val repositories = ArrayList<String>()

Both are mutable list, then what is the point of two keywords mutableListOf or ArrayList?

or is there any major difference?

over 4 years ago · Santiago Trujillo
4 answers
Answer question

0

Under the covers, both mutableListOf() and arrayListOf() create an instance of ArrayList. ArrayList is a class that happens to implement the MutableList interface.

The only difference is that arrayListOf() returns the ArrayList as an actual ArrayList. mutableListOf() returns a MutableList, so the actual ArrayList is "disguised" as just the parts that are described by the MutableList interface.

The difference, in practice, is that ArrayList has a few methods that are not part of the MutableList interface (trimToSize and ensureCapacity).

The difference, philosophically, is that the MutableList only cares about the behaviour of the object being returned. It just returns "something that acts like a MutableList". The ArrayList cares about the "structure" of the object. It allows you to directly manipulate the memory allocated by the object (trimToSize).

The rule of thumb is that you should prefer the interface version of things (mutableListOf()) unless you actually have a reason to care about the exact details of the underlying structure.

Or, in other words, if you don't know which one you want, choose mutableListOf first.

over 4 years ago · Santiago Trujillo Report

0

As you can see in sources:

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

So, there is no difference, just a convenience method.

over 4 years ago · Santiago Trujillo Report

0

mutableListOf<T>() is an inline function invocation that returns a MutableList<T>. As of today, mutableListOf does return an instance of ArrayList.

ArrayList<String>() is a constructor invocation and cannot be inlined.

In other words, given:

 val a = mutableListOf<String>()
 val b = ArrayList<String>()
  • a is of type MutableList<String>
  • b is of type ArrayList<String>

At runtime, both a and b will hold an instance of ArrayList.

Note that inlining is particularly useful when combined with type reification, which justifies the existence of listOf, mutableListOf and the like.

over 4 years ago · Santiago Trujillo Report

0

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".

In practice, in the current implementation of Kotlin compiling to the JVM, calling mutableListOf will produce an ArrayList, and there's no difference in behaviour: once the list is built, everything will behave the same.

Now, let's say that a future version of Kotlin changes mutableListOf to return a different type of list.

Likelier than not, the Kotlin team would only make that change if they figure the new implementation works better for most use cases. mutableListOf would then have you using that new list implementation transparently, and you'd get that better behaviour for free. Go with mutableListOf if that sounds like your case.

On the other hand, maybe you spent a bunch of time thinking about your problem, and figured that ArrayList really is the best fit for your problem, and you don't want to risk getting moved to something sub-optimal. Then you probably want to either use ArrayList directly, or use the arrayListOf factory function (an ArrayList-specific analogue to mutableListOf).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!