Kotlin – Create Empty List

To create an empty list in Kotlin, we have two ways. One is using listOf() function and the other is using emptyList() function.

The syntax to create an empty list using listOf() function is

</>
Copy
myEmptyList: List<T> = listOf()

where T is the type of elements in this List.

The syntax to create an empty list using emptyList() function is

</>
Copy
myEmptyList: List<T> = emptyList()

where T is the type of elements in this List.

We can also specify the type with the function, instead of List<T> as shown in the following.

</>
Copy
myEmptyList = listOf<T>()
myEmptyList = emptyList<T>()

Examples

In the following program, we will create an empty list of integers using listOf().

Main.kt

</>
Copy
fun main(args: Array<String>) {
    var myEmptyList: List<Int> = listOf()
    println("List contents : $myEmptyList")
}

Output

List contents : []

In the following program, we will create an empty list of Strings using emptyList().

Main.kt

</>
Copy
fun main(args: Array<String>) {
    var myEmptyList: List<String> = emptyList()
    println("List contents : $myEmptyList")
}

Output

List contents : []

Conclusion

In this Kotlin Tutorial, we learned how to create an empty list using listOf() and emptyList() functions, with the help of examples.