Kotlin MutableList: How to Create an Empty List Without Losing Your Mind

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: The Paradox of the Empty List in Kotlin

\n\n

Imagine: 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?

\n\n

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.

\n\n

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!

\n\n

1. Why Doesn't Just MutableList() Work?

\n\n

Let's debunk the myth right away. If you try to write:

\n\n
// Compilation error!\nval list = MutableList()
\n\n

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

\n\n

Fortunately, we have several elegant ways to bypass this limitation.

\n\n

2. The Simplest Way: mutableListOf()

\n\n

This is the king of empty lists. A standard factory function from the Kotlin standard library:

\n\n
val emptyList = mutableListOf<String>()\nprintln(emptyList) // Output: []\nprintln(emptyList.size) // Output: 0
\n\n

Why this is great:

\n
    \n
  • No need to specify the type if it can be inferred from context
  • \n
  • Creates a MutableList with an ArrayList type under the hood
  • \n
  • Reads as "create a mutable list from nothing"
  • \n
\n\n

Adding elements is a pleasure:

\n\n
val numbers = mutableListOf<Int>()\nnumbers.add(1)\nnumbers.add(2)\nnumbers.add(3)\nprintln(numbers) // [1, 2, 3]
\n\n

This method is suitable for 90% of cases. Remember it as the primary one.

\n\n

3. The Classic ArrayList() — The King Returns

\n\n

If you come from Java, this method will feel familiar. Kotlin is fully compatible with the Java Collections Framework, so you can use ArrayList directly:

\n\n
val list = ArrayList<String>()\nprintln(list) // []
\n\n

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

\n\n

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
\n\n

4. The Tricky Way: MutableList(size) { defaultValue }

\n\n

Sometimes 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\n

This 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\n

For a complete zero, use

Blogs

Book Recommendations