Partner – Jetbrains – NPI EA (cat = Kotlin Collections)
As a seasoned developer, you’re likely already familiar with
Spring. But Kotlin can take your developer experience with Spring
to the next level!
Join the >> "Advanced Kotlin
Techniques for Spring Developers" webinar and learn how to:
Add new functionality to existing classes with Kotlin extension
Use Kotlin bean definition DSL.
Better configure your application using lateinit.
Use sequences and default argument values to write more
By the end of this talk, you’ll have a deeper understanding of
the advanced Kotlin techniques that are available to you as a
Spring developer, and be able to use them effectively in your
In this quick tutorial, we’ll discuss two ways of creating mutable lists in Kotlin.
2. Creating a Mutable List in Kotlin
We can create an ArrayList implementation of the mutable list simply by using a constructor:
val arrayList = ArrayList<String>()
This is Kotlin’s implementation of dynamic arrays.
We can also create a mutable list by using a dedicated method:
val mutableList = mutableListOf<String>()
Let’s peek at the current (Kotlin 1.4.10) implementation of the mutableListOf<T>() method:
* Returns an empty new [MutableList].
* @sample samples.collections.Collections.Lists.emptyMutableList
public inline fun <T> mutableListOf(): MutableList<T> = ArrayList()
As we can see, it simply returns an instance of ArrayList<T>(), which is the default implementation of List. So the implementation details of the mutable list have been abstracted away for us.
In this quick article, we saw two ways to create a mutable list in Kotlin.
Our first approach explicitly creates an instance of a particular class by calling its constructor — ArrayList<T>().
In the second approach, we just want a mutable list and don’t really care about the implementation, and therefore, we can’t make any assumptions about the actual implementation used under-the-hood.
However, as we noted, both approaches have the same result in the current version of Kotlin, as both will create an ArrayList.