This article explores different ways to initialize an empty List in Kotlin.

There are several ways to initialize an empty list, as discussed below:

1. Using listOf() function

If you need an immutable empty list instance, you can use the listOf() function, as shown below:

Download Code

 
The declaration can be further shortened to:

Download Code

2. Using mutableListOf() function

To get a mutable list, you should use the mutableListOf() function.

Download Code

 
This can be shortened to:

Download Code

3. Using arrayListOf() function

If you need an ArrayList implementation of list, consider using the arrayListOf() function.

Download Code

 
Alternatively, you can write it as following to improve readability:

Download Code

4. Using ArrayList Constructor

You can use the ArrayList constructor to get an empty ArrayList instance of the List interface.

Download Code

 
Similar to the previous approach, we can rewrite this as:

Download Code

5. Using LinkedList Constructor

Finally, if you need a LinkedList implementation of the List interface, you can use the LinkedList constructor to get an empty mutable instance of a linked list.

Download Code

 
This is equivalent to writing:

Download Code

That’s all about initializing an empty list in Kotlin.