1. Introduction

One of the most common data structures in Kotlin is the mutable List, which allows for the storage and manipulation of data. Therefore, it’s not uncommon to be faced with situations in which we seek to change a value in a mutable list.

In this article, we’re going to explore various approaches to changing a value in a Mutable List in Kotlin.

2. Using the set() Method

A simple way to change the value of an element within a mutable list is to use the set() method. This method takes two arguments, the index of the element we wish to change and the new value we wish to assign to that element:

@Test
fun `changes mutable list value using set method`() {
    val mutableList = mutableListOf("kotlin", "java", "android")
    mutableList.set(1, "swift")
        
    assertEquals(mutableListOf("kotlin", "swift", "android"), mutableList)
}

In the above unit test, we first create a mutable list of strings containing “kotlin”, “java”, and “android”. We then use the set() method to change the value of the element at index 1 from “java” to “swift”. Finally, we assert that the mutable list now contains the expected values, including the new value “swift” at index 1.

3. Using the Indexed Access Operator

Another interesting approach is to use the indexed access operator. This operator allows us to access an element within a mutable list by its index and then assign a new value to it:

@Test
fun `changes mutable list value using indexed access operator`() {
    val mutableList = mutableListOf("kotlin", "java", "android")
    mutableList[1] = "swift"

    assertEquals(mutableListOf("kotlin", "swift", "android"), mutableList)
}

In this code snippet, after creating a mutable list of strings, we use the indexed access operator to access the element at index position 1, which contains the value “java”. We then assign a new value of “swift” to the element at this index position. Finally, we use the assertEquals() method to verify that the mutable list contains the expected values.

4. Using the map() Method

The map() method is yet another approach we can use to update a value in a list. However, this method does not perform the change on the list directly but instead returns a new list that contains all elements of the list including the new value we wish to update.

It takes a lambda function as an argument that is executed for each element in the mutable list. The lambda function takes one argument – the current element. Within the lambda function, we can check if the current element is the one we want to change and then return a new value for it. The map() method returns a new list with the updated values:

@Test
fun `creates new list with updated value using the map method`() {
    val mutableList = mutableListOf("kotlin", "java", "android")
    val updatedList = mutableList.map { element ->
        if (element == "java") {
            "swift"
        } else {
            element
        }
    }

    assertEquals(mutableListOf("kotlin", "swift", "android"), updatedList)
}

In the code above, we use the map() method on a mutable list of strings to iterate over each element and return a new value for each element. Within the lambda function, we check if the current element is “java” and if that’s the case, we return a new value of “swift”. Otherwise, we retain the original value. We then conclude by asserting that the updated list contains the expected values.

This approach has a major downside: It returns a new list with the updated values rather than changing the value in place. This means creating a new list using the map() method could consume a significant amount of memory. It could also impact performance if the mutable list is large.

5. Using the replaceAll() Method

Kotlin also provides a replaceAll() method that can be used to update the value of an element in a mutable list. This method allows us to replace all occurrences of a particular value in the list with a new value:

@Test
fun `changes mutable list value using the replace method`() {
    val mutableList = mutableListOf("kotlin", "java", "android")
    mutableList.replaceAll { if (it == "java") "swift" else it }
        
    assertEquals(mutableListOf("kotlin", "swift", "android"), mutableList)
}

In this unit test, we create a mutable list containing three elements – “kotlin”, “java”, and “android”. We then use the replaceAll() method to replace all occurrences of “java” with “swift”. Finally, we use assert our mutable list now contains the expected values.

The replaceAll() method can make global changes to a mutable list. However, there is a potential downside: It will modify all occurrences of the specified value in the list. This can happen even if those occurrences are intended to be different. This can lead to unexpected results if we’re not aware of this behavior.

6. Conclusion

In this article, we’ve explored various ways to change a value in a mutable list. While some are more simple and straightforward, such as with the set() and indexed access operator approaches, the map() and replaceAll() approaches impose some limitations that we must keep in mind if we consider using them in our programs.

Overall, these approaches are easy to use, and our choice of which one to adopt in our programs depends on our needs.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments