Updated February 22, 2023
Introduction to Kotlin Empty List
Kotlin empty list is a generic ordered collection element. It contains two types of lists, i.e., mutable and immutable. The read-only list is created by using the listOf functions; those elements cannot be modified,, and a the mutable list is created with the method where we can modify or alter the elements from the list. In kotlin, the list is an item in an ordered collection; we use standard library functions.
Key Takeaways
- In kotlin, we are creating the empty list by using the listOf() and emptyList() function. Both functions are used to create an empty list in kotlin.
- When creating an empty list in kotlin, we need to give the specified name to the empty list.
What is Kotlin Empty List?
It is a generic collection of elements and interfaces. The list interface will inherit from the T class. The empty list is immutable, and it will support the read functionalities. We need to use the listOf () function to use the empty list interface. The elements list follows the insertion order sequence and contains the index number in the the same array. There are different types of functions available in the empty list interface. The empty list will return the read-only list; the returned list in kotlin is serializable. It is very important.
How to Create Kotlin Empty List?
To create an empty list in kotlin, there are two ways to create an empty list in kotlin as follows:
- listOf() function
- emptyList() function
Creating an empty list in kotlin by using any of the functions contains different syntax to create an empty list. The below example shows syntax and examples of creating an empty list by using listOf() function as follows.
1. listOf() function
Below syntax shows to create an empty list by using the listOf () function. We need to give the list name when creating an empty list in kotlin. In the below example, first, we are giving the empty list name. After giving the empty list name,, we use the T value as nothing but the type of elements in the list. Also, we need to give a keyword of the listOf function which we are creating the empty list.
Syntax:
Empty_list_name: list <T> = listOf ()
The below example shows listOf() function as follows. In the below example, we are creating an empty list name as empty_list. In the below example, we are creating the empty list of integer values by using the function as listOf() as follows.
Example:
Code:
fun main(args: Array<String>)
{
var empty_list: List<Int> = listOf()
println("List : $empty_list")
}
Output:
2. emptyList() function
Below syntax shows to create an empty list by using the emptyList() function. We need to give the empty list name when creating an empty list in kotlin. In the below example, first, we are giving the empty list name. After giving the empty list name, we are using the T value.
Syntax:
Empty_list_name: list <T> = emptyList()
Below example shows emptyList() function as follows. In the below example, we are creating an empty list name as empty.
Example:
Code:
fun main(args: Array<String>)
{
var empty: List<String> = emptyList()
println("List : $empty")
}
Output:
Function
The empty list is not a constructor, but this function returns the list immutable for implementation. The reason such a function exists in it is to save the allocations. The empty list will return an instance that was singleton in every time. Therefore, the empty list reads better than the listOf function used with the default parameter value. The example below shows the empty list using the listOf function as follows. In the below example, we have created the empty list name as emptyList_fun.
Example:
Code:
fun main(args: Array<String>) {
val emptyList_fun = listOf<String>()
println ("emptyList_fun.isEmpty() is ${emptyList_fun.isEmpty ()}")
println (emptyList_fun)
}
Output:
The example below shows a function to create an empty list using the emptyList function. This function is very useful while creating an empty list. In the below example, we are creating the empty list name as list_fun as follows.
Example:
Code:
fun main(args: Array<String>) {
var list_fun: List<String> = emptyList()
println ("Empty list function : $list_fun")
}
Output:
Kotlin Empty List Initialize
The array list class is used to create an empty array list. It is dynamic, so we can say that it will automatically expand when we add the data to it. The array list is an element of the order sequence. Unlike a simple array, the array list will contain data in multiple types of data. Below is the function definition of array list as follows.
Syntax:
fun <Types> arrayListOf (): ArrayList (Types)
The above syntax will return a new list of arrays. If suppose we provide the number as an argument, it will return an array list using specified elements. The below example shows we can initialize an empty array into the kotlin. In the following example, we are creating a dynamic array list, and after creating the list, we are initializing the same in our code.
Example:
Code:
fun main(args: Array<String>) {
val initialize_list = ArrayList<String>()
println ("Array list: " + initialize_list)
initialize_list.add ("ABC")
initialize_list.add ("PQR")
initialize_list.add ("XYZ")
initialize_list.add ("LMN")
initialize_list.add ("CBZ")
initialize_list.add ("BCD")
initialize_list.add ("OPQ")
println ("Elements List: " + initialize_list)
}
Output:
The below example shows initializing an empty list by using the mutable list as follows.
Code:
fun main() {
val mlist : MutableList<Int> = mutableListOf()
println(mlist)
}
Output:
The below example shows initialize an empty list by using an array list as follows.
Code:
fun main()
{
val alist : ArrayList<Int> = ArrayList ()
println(alist)
}
Output:
FAQ
Given below is the FAQ mentioned:
Q1. How we can create an empty list by using listOf function?
Answer:
The below example shows to create the empty list by using list of function.
Code:
fun main(args: Array<String>)
{
var example: List<String> = listOf()
println ("List : $example")
}
Output:
Q2. How we can create an empty list by using emptyList function?
Answer:
The below example shows to create an empty list by using emptyList of function.
Code:
fun main(args: Array<String>) {
var example: List<String> = emptyList()
println ("List : $example")
}
Output:
Q3. What is mutable list in kotlin empty list?
Answer:
We can initialize the empty list by using the mutable list function in kotlin. We are initializing any empty list by using a mutable list.
Conclusion
The empty list is immutable, and it will support the read functionalities. To use the empty list interface, we need to use the function called as listOf(). It is a generic ordered collection element. It contains two types of list i.e. mutable list and immutable lists.
Recommended Articles
This is a guide to Kotlin Empty List. Here we discuss the introduction and two ways to create an empty list in kotlin along with examples and code implementation. You may also have a look at the following articles to learn more –