Kotlin Mutable Vs Immutable List

--

The Kotlin List helps the developer read and write data locally. There are two types of lists available in Kotlin. A list that is used only for reading purposes is called an immutable list. A mutable list is a list where the developer can write data.

Lists are part of the Kotlin Collection. Kotlin collections are defined in three forms. These are Lists, Sets , Maps. In this article, we will learn about Kotlin lists and their functionality.

Kotlin Mutable Vs Immutable List
Kotlin Mutable Vs Immutable List

Create an immutable list:

listOf() function is used to create an immutable list in Kotlin. Let’s try out an example.


fun main(args: Array<String>) {
//initialize the list
var listOfAnimal = listOf<String>("Cow", "Dog", "Goat")
// print the list
println(listOfAnimal)
}
Output :
[Cow, Dog, Goat]

Now if we want to add some animal names to the above example list, we will get an error like below. Because mutable lists have only read functionality, we can’t add, modify, or delete elements from a mutable list.

fun main(args: Array<String>) {
//initialize the list
var listOfAnimal = listOf<String>("Cow", "Dog", "Goat")
// trying to add an element
listOfAnimal.add(“Rat”)
// print the list
println(listOfAnimal)
}
Output :
Unresolved reference : add

Retrieve the value in the immutable list:

Now we will learn how to retrieve values from a list with the help of an index. Remember, indexes start at zero (0). Zero (0) means the first element of a list. There are two ways we can retrieve values from lists. The first is using third brackets [], and the second is using the get() function. Let's try out an example.

fun main(args: Array<String>) {
//initialize the list
var listOfAnimal = listOf<String>("Cow", "Dog", "Goat")
// using [] brackets
println(listOfAnimal[0])
// using get function
println(listOfAnimal.get(1))
}
Output :
[Cow]
[Dog]

Find the size of the list:

To find the size of a list, we will use the size property. Let’s try out an example. If you need more details about this topic, then check out the link. Kotlin Collections

fun main(args: Array<String>) {
// initialize list
var listOfAnimal = listOf<String>("Cow", "Dog", "Goat")
// print the size of list
println("Size is :" + listOfAnimal.size)
}
Output :
Size is: 3

Create a mutable list:

mutableListOf() function is used to create a mutable list in Kotlin. Let’s try out an example.

fun main(args: Array<String>) {
// initialize the list
var listOfAnimal = mutableListOf<String>("Cow", "Dog", "Goat")
// print the list
println(listOfAnimal)
}
Output :
[Cow, Dog, Goat]

Add an element to the mutable list:

Now we will learn how to add elements to a mutable list. There is a function called add(). Let’s try out an example.

fun main(args: Array<String>) {
// initialize the list
var listOfAnimal = mutableListOf<String>("Cow", "Dog", "Goat")
// add value in a list
listOfAnimal.add("Rat")
// print the list
println(listOfAnimal)
}
Output:

[Cow, Dog, Goat, Rat]

Modify or update an element in a mutable list:

Now we will learn how to modify or update the value of an element in a mutable list. We will use the set(index: Int, element: String) function to perform the operation. Let's try out an example.

fun main(args: Array<String>) {
//initialize the list
var listOfAnimal = mutableListOf<String>("Cow", "Dog", "Goat")
// modify the value of Dog to Rabbit
listOfAnimal.set(1 , "Rabbit")
// print the list
println(listOfAnimal)
}
Output :

[Cow, Rabbit, Goat]

Remove or delete an element in a mutable list:

Now we will learn how to remove or delete an element from a list. There are so many removal methods used to remove elements from a list. Here we will cover the removeAt(index: Int) function. This function is used when we need to remove an element from a specific position in a list. Let's try out an example.

fun main(args: Array<String>) {
//initialize the list
var listOfAnimal = mutableListOf<String>("Cow", "Dog", "Goat")
// remove at index 1 value
listOfAnimal.removeAt(1)
// print the list
println(listOfAnimal)
}
Output :
[Cow, Goat]

We can also remove an element without using an index. Suppose we need to remove the “Goat” element from the above example, then we will use the remove(element: String) function. Let’s try out an example.

fun main(args: Array<String>) {
//initialize the list
var listOfAnimal = mutableListOf<String>("Cow", "Dog", "Goat")
// remove the Goat value from the list
listOfAnimal.remove(“Goat”)
// print the list
println(listOfAnimal)
}
Output :
[Cow, Dog]

Remove the first element in a mutable list:

There are two ways we can remove the first element from a mutable list. These are as follows:

1. removeAt(Index: Int): Here we have to pass zero(0) because arrays start with the index of zero(0).

2. removeFirst(): Here we do not need to pass any index. Using this function, we can easily remove the first element of a list.

Let’s try out an example. If you need more details about this topic, then check out the link. Kotlin Collections

fun main(args: Array<String>) {
// initialize the list
var listOfAnimal = mutableListOf<String>("Cow", "Dog", "Goat")
// remove the first element from the list
listOfAnimal.removeFirst()
// print the list
println(listOfAnimal)
}
Output :
[Dog, Goat]

--

--

Anupam Singh -> Android Dev | Guest Post Service

I am a Mobile App Developer and Content Writer. More than 10 years of experience. Contact us for app and web development, guest posting, and content writing.