You already know quite a lot about collections. In the previous topics, you got familiar with List. In this topic, we return to an already familiar MutableList collection. You already know how to work with MutableList, so we recap this knowledge and discuss when and how it is used, where it differs from List and what its main purpose is.
Introduction
Let's recap: List is a collection. List has a lot of methods and properties out-of-the-box. You cannot make changes to the elements of a List because it is immutable. Now we will learn about its mutable twin brother.MutableList is another version of List. It allows duplicates and stores elements in a specific order. In contrast to List, MutableList is a mutable or modifiable collection that allows you to add and remove elements. MutableList includes functions such as add, remove, and clear.
Imagine you decided to keep a List of all the places you've been to:
val places = listOf<String>("Paris", "Moscow", "Tokyo")
println(places) // output: [Paris, Moscow, Tokyo]
You keep travelling, and your most recent trip was to Saint Petersburg. You want to add it to your List of places, but there is a problem: you cannot add another item to the List because it is immutable. You can do this by reassigning, but this is a slow and inefficient way:
var places = listOf<String>("Paris", "Moscow", "Tokyo") // note var keyword
places += "Saint-Petersburg" // reassignment, slow operation
println(places) // output: [Paris, Moscow, Tokyo, Saint-Petersburg]
This is where MutableList comes to our rescue. As we said before, MutableList supports adding elements. So, let's switch to MutableList and add one more element:
val places = mutableListOf<String>("Paris", "Moscow", "Tokyo")
places.add("Saint-Petersburg")
println(places) // output: [Paris, Moscow, Tokyo, Saint-Petersburg]Initializing
Here is how you can initialize MutableList:
val cars = mutableListOf("Ford", "Toyota", "Audi", "Mazda", "Tesla")
println(cars) // output: [Ford, Toyota, Audi, Mazda, Tesla]
That is it! Here, we didn't even need to specify the type for our objects because it can be derived from the context. However, note that if you create an empty MutableList, you do have to specify the type:
val cars = mutableListOf<String>()
println(cars) // output: []
You can also transform List into a MutableList with the help of the function toMutableList(). If you want to transform MutableListinto a List, you can use toList():
val cars = listOf("Ford", "Toyota").toMutableList()
cars.add("Tesla")
println(cars) // output: [Ford, Toyota, Tesla]
val carsList = cars.toList()Adding and replacing elements
MutableList has the same properties and methods as List: size, get(index), isEmpty(), indexOf(element), contains(element), and so on.
Since MutableList is special because it can be modified, it has additional functionality for changing the contents:
add(element)is a method that adds an extra element to your list.set(index, element)replaces the element at the specified position with the specified element. Laconic form:mutableList[index] = elementaddAll(elements)adds all of the elements of the specified collection to the end of the list.
Let's take a look at some examples. Imagine that you're about to go get groceries, so you're making a List of products you need:
val products = listOf("Milk", "Cheese", "Coke")
You had a change of heart: suddenly, you decided that you also need to get some chips, and maybe get water instead of milk. Let's update our list of products:
val finalList = products.toMutableList()
finalList.add("Chips")
finalList[0] = "Water" // or finalList.set(0, "Water")
println(finalList) // output: [Water, Cheese, Coke, Chips]
Then, let's say your dad came in and gave you his grocery list. Okay, let's add these products to our list as well:
val products = mutableListOf("Milk", "Cheese", "Coke")
val dadsProducts = listOf("Banana", "Watermelon", "Apple")
products.addAll(dadsProducts)
println(products) // output: [Milk, Cheese, Coke, Banana, Watermelon, Apple]Removing elements
You might also need to remove some or all elements from your list. Let's see how this can be done:
removeAt(index)removes an element at a specified index.remove(element)remove the first occurrence of the specified element.clear()removes all elements from the current collection.
Let's go back to our grocery list. As you were getting dressed, you gradually realized that you actually had all this food in your fridge. One by one, you decided to clear your list of these products:
val products = mutableListOf("Milk", "Cheese", "Coke")
products.removeAt(0)
println(products) // output: [Cheese, Coke]
products.remove("Coke")
println(products) // output: [Cheese]
products.clear()
println(products) // output: []
This is not an exhaustive list of methods you can use. To learn about other MutableList methods, check out kotlinlang.org.
Iterating through elements
You can iterate through the elements in MutableList with the help of the for loop. Here is an example:
val products = mutableListOf("Cheese", "Milk", "Coke")
for (product in products) {
println("$product")
}
// Cheese
// Milk
// Coke
We iterated through our MutableList and printed the name of each product.
Conclusion
Now you know the difference between a List and a MutableList. List is immutable, so use it when you don't want the content to change. MutableList is useful when you know that its contents will need some modifications in the future. Now you know how to initialize a MutableList and add, replace, and remove elements from it. You also know how to make a List mutable and iterate through its elements using the for loop. Note that you can solve problems on this topic with immutable lists if you're an avid programmer, but while we're just learning, we'll use mutable lists.