Introduction: The Paradox of the Empty List in Kotlin
\n\nImagine: you're writing in Kotlin, and you need to create a list that will be dynamically populated. You know that List is an immutable collection, and MutableList is just what the doctor ordered. But as soon as you write val list = MutableList(), the compiler throws an error. Sound familiar?
This question is one of the most popular on Stack Overflow among Kotlin beginners (and not only). Thousands of developers stumble over the same rake: how to properly initialize an empty MutableList? It seems like a small thing, but without it, no dynamic algorithm works.
In this article, we'll cover all the legal ways to create an empty mutable list in Kotlin. From simple life hacks to advanced techniques. You'll learn why mutableListOf() is your best friend, and ArrayList() is an old but reliable companion. Let's go!
1. Why Doesn't Just MutableList() Work?
\n\nLet's debunk the myth right away. If you try to write:
\n\n// Compilation error!\nval list = MutableList()\n\nThe compiler will rightly complain. Why? Because MutableList is an interface, not a concrete class. An interface cannot be instantiated. It's like trying to create "just a car" without specifying the make and model. Kotlin requires a concrete implementation.
Fortunately, we have several elegant ways to bypass this limitation.
\n\n2. The Simplest Way: mutableListOf()
\n\nThis is the king of empty lists. A standard factory function from the Kotlin standard library:
\n\nval emptyList = mutableListOf<String>()\nprintln(emptyList) // Output: []\nprintln(emptyList.size) // Output: 0\n\nWhy this is great:
\n- \n
- No need to specify the type if it can be inferred from context \n
- Creates a
MutableListwith anArrayListtype under the hood \n - Reads as "create a mutable list from nothing" \n
Adding elements is a pleasure:
\n\nval numbers = mutableListOf<Int>()\nnumbers.add(1)\nnumbers.add(2)\nnumbers.add(3)\nprintln(numbers) // [1, 2, 3]\n\nThis method is suitable for 90% of cases. Remember it as the primary one.
\n\n3. The Classic ArrayList() — The King Returns
\n\nIf you come from Java, this method will feel familiar. Kotlin is fully compatible with the Java Collections Framework, so you can use ArrayList directly:
val list = ArrayList<String>()\nprintln(list) // []\n\nBut there's a nuance: ArrayList is a concrete implementation of the MutableList interface. If you later want to change the implementation (e.g., to LinkedList), you'll have to change the variable type. In this sense, mutableListOf() is more flexible because it returns a MutableList.
When to use ArrayList():
\n- \n
- When maximum performance is needed (ArrayList is faster than LinkedList for index-based access) \n
- When working with Java libraries that expect specifically an ArrayList \n
- When you consciously choose a specific implementation \n
4. The Tricky Way: MutableList(size) { defaultValue }
\n\nSometimes an empty list isn't exactly "empty." You might need to create a list with a predetermined size but with empty values. For this, there's a constructor overload:
\n\n// Create a list of 5 elements, each being an empty string\nval list = MutableList(5) { "" }\nprintln(list) // [, , , , ]\nprintln(list.size) // 5\n\nThis isn't exactly an empty list, but a list initialized with "placeholders." Useful when you know the exact number of elements but will overwrite them.
\n\nFor a complete zero, use
Blogs
Book Recommendations
Read also
Numbers with Leading Zeros in PHP: A Simple Solution
Why JavaScript Became the King of the Web: A Success Story
Taming Dependencies: How to Install a Specific Version of a Composer Package
Deploying a C++ Application on VPS: A Step-by-Step Guide
Serverless Functions in Go on AWS Lambda: Advanced Techniques and Optimization