388

Seems so simple, but, how do I initialize Kotlin's MutableList to empty MutableList?

I could hack it this way, but I'm sure there is something easier available:

var pusta: List<Kolory> = emptyList()
var cos: MutableList<Kolory> = pusta.toArrayList()
Ziem
  • 6,579
  • 8
  • 53
  • 86
ssuukk
  • 8,200
  • 7
  • 35
  • 47

6 Answers6

667

You can simply write:

val mutableList = mutableListOf<Kolory>()

This is the most idiomatic way.

Alternative ways are

val mutableList : MutableList<Kolory> = arrayListOf()

or

val mutableList : MutableList<Kolory> = ArrayList()

This is exploiting the fact that java types like ArrayList are implicitly implementing the type MutableList via a compiler trick.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
  • Do you need to import anything? When I write this in my current project I get unresolved reference arrayListOf and same if I try mutableListOf – vextorspace Mar 14 '17 at 19:42
  • Do you have the stdlib in your classpath? – Kirill Rakhman Mar 15 '17 at 08:57
  • I just had to switch my kotlin_version flag in my build.gradle back to 1.1.0 instead of 1.1.1 – vextorspace Mar 16 '17 at 14:35
  • What if I know the size of the new MutableList ? For ArrayList, I can do : ArrayList(24), for example, if I think that 24 is a good start for it, that it will probably won't need more than it. – android developer Apr 23 '18 at 08:08
  • @androiddeveloper Then you can just call the `ArrayList` constructor: `val mutableList = ArrayList(24)`. `ArrayList` implements `MutableList` – Kirill Rakhman Apr 23 '18 at 09:00
  • @KirillRakhman Seems like it: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/ . But I don't get then why would I need to use MutableList at all. Do they make it in case they would find a nicer implementation in the future, that might not be ArrayList or another known class? – android developer Apr 23 '18 at 09:18
  • @androiddeveloper The same reason, any interface exists. See https://stackoverflow.com/questions/4456424/what-do-programmers-mean-when-they-say-code-against-an-interface-not-an-objec – Kirill Rakhman Apr 23 '18 at 09:55
  • @KirillRakhman Well they could use "List" instead, just as on Java. I don't see anything special in MutableList vs List . No ? – android developer Apr 23 '18 at 09:58
  • @androiddeveloper please see https://stackoverflow.com/questions/46445909/difference-between-mutablelist-and-list-in-kotlin – Kirill Rakhman Apr 23 '18 at 09:59
  • @KirillRakhman This is weird. The docs of "List" is different on Java: https://docs.oracle.com/javase/8/docs/api/java/util/List.html . You can add, remove and do whatever you wish there. They changed it here. They removed so many methods... This is so confusing. They could instead just create an interface "ReadOnlyList", instead of ruining an already known class by using its name on Java and give it a different role... – android developer Apr 23 '18 at 10:03
  • 2
    @androiddeveloper that's because `kotlin.collections.List`is *not* `java.utils.List`. Kotlin has a mechanism for mapping some built-in java types. Please refer to https://kotlinlang.org/docs/reference/java-interop.html#mapped-types and similar SO questions. The comment section is not appropriate for discussing this in detail. – Kirill Rakhman Apr 23 '18 at 11:01
  • Is there a way to add one or two elements of the list when creating the list itself? – Mohanakrrishna Apr 08 '19 at 09:59
  • 2
    @Mohanakrrishna yes, the functions support passing arguments. – Kirill Rakhman Apr 08 '19 at 10:02
25

Various forms depending on type of List, for Array List:

val myList = mutableListOf<Kolory>() 
// or more specifically use the helper for a specific list type
val myList = arrayListOf<Kolory>()

For LinkedList:

val myList = linkedListOf<Kolory>()
// same as
val myList: MutableList<Kolory> = linkedListOf()

For other list types, will be assumed Mutable if you construct them directly:

val myList = ArrayList<Kolory>()
// or
val myList = LinkedList<Kolory>()

This holds true for anything implementing the List interface (i.e. other collections libraries).

No need to repeat the type on the left side if the list is already Mutable. Or only if you want to treat them as read-only, for example:

val myList: List<Kolory> = ArrayList()
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
  • What if I know the size of the new MutableList ? For ArrayList, I can do : `ArrayList(24)`, for example, if I think that 24 is a good start for it, that it will probably won't need more than it. – android developer Apr 23 '18 at 07:32
  • @androiddeveloper View the docs for the list constructors or Java API for the underlying lists and you will see options for what you want. – Jayson Minard Apr 23 '18 at 14:14
  • you forgot about `mutableListOf`. The correct would be: `val myList = arrayListOf() // same as // val myList = mutableListOf()` – user924 Jan 14 '19 at 11:30
  • linkedListOf was for earlier versions of Kotlin also I believe that Kotlin no longer has LinkedList – Mr.Q May 15 '21 at 14:03
10

I do like below to :

var book: MutableList<Books> = mutableListOf()

/** Returns a new [MutableList] with the given elements. */

public fun <T> mutableListOf(vararg elements: T): MutableList<T>
    = if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements, isVarargs = true))
Kim
  • 980
  • 1
  • 15
  • 29
7

Create Mutable list of nullable String in kotlin

val systemUsers: MutableList<String?> = mutableListOf()
FEELIX
  • 407
  • 4
  • 8
3

It is absolutely valid to use the MutableList() function of the Kotlin collections that intentionally looks like a constructor. This function is in general very useful to know because it can also consume an initialization function that pre-fills all values of a (non-empty) list.

val emptyListOfTypeUnit = MutableList(0) {}

val emptyListOfTypeInt = MutableList(0) { 0 }
val verboseEmptyListOfTypeInt = MutableList<Int>(0) { 0 }

val emptyListOfTypeString = MutableList(0) { "" }
val verboseEmptyListOfTypeString = MutableList<String>(0) { "" }

val emptyListOfTypeKolory = MutableList(0) { Kolory() }
val verboseEmptyListOfTypeKolory = MutableList<Kolory>(0) { Kolory() }

Disclaimer: I was introduced to this in the Jetbrains Academy course for Kotlin developers, which is unfortunately not public. Therefore, I cannot link a reference here. Sorry.

Kekzpanda
  • 752
  • 6
  • 15
  • Thanks. I would have never guesses that creating an empty list is so complicated. Not to mention that having to pass an empty lambda is quite imperformant considering that ever lambda is a complete class underneath. – Martin Jan 18 '22 at 08:44
2

Use

var cos = mutableListOf<Kolory>()

Or

// here you have to specify init lambda, although it is useless
var cos = MutableList(0) { Kolory() }

These versions work on Kotlin Multiplatform, and therefore need no platform-specific dependencies.

llesha
  • 423
  • 1
  • 15