In Kotlin, when we work with collections like lists or sets, sometimes we need to change or update their contents. If a collection allows us to modify its elements, we call it a MutableCollection. This means we can add, remove, or update its elements anytime. In this article, we will explore the different ways we can perform these write operations in a Kotlin MutableCollection.
1. Adding Elements
add() Function
We use the add() function to insert a new element into the collection. The element is added to the end of the collection.
Example:
fun main() {
val numbers = mutableListOf(11, 12, 13, 14)
numbers.add(15)
println(numbers)
}
Output:
[11, 12, 13, 14, 15]addAll() Function
The addAll() function helps us add multiple elements at once. We can pass an Iterable, Sequence, or Array to this function.
Example:
fun main() {
val list1 = mutableListOf(1, 2, 2, 13, 14)
val list2 = listOf(5, 6)
list1.addAll(list2)
println(list1)
list1.addAll(listOf(7, 8))
println(list1)
}
Output:
[1, 2, 2, 13, 14, 5, 6]
[1, 2, 2, 13, 14, 5, 6, 7, 8]
plusAssign (+=) Operator
We can also use the += operator to add an element or a group of elements to the collection.
Example:
fun main() {
val items = mutableListOf("one", "two", "seven", "four", "five")
items += "three"
println(items)
}
Output:
[one, two, seven, four, five, three]2. Removing Elements
remove() Function
The remove() function deletes the first occurrence of the given element from the collection. If the element is not found, nothing happens.
Example:
fun main() {
val numbers = mutableListOf(11, 22, 33, 44, 33)
numbers.remove(33)
println(numbers)
}
Output:
[11, 22, 44, 33]
removeAll() Function
We can use removeAll() to remove all elements that match the given collection or condition.
Example:
fun main() {
val items = mutableListOf("one", "two", "three", "four")
items.removeAll(listOf("one", "two", "four"))
println(items)
}
Output:
[three]minusAssign (-=) Operator
The -= operator removes the specified element or collection of elements.
Example:
fun main() {
val items = mutableListOf("one", "two", "three", "four")
items -= "two"
println(items)
}
Output:
[one, three, four]retainAll() Function
This function keeps only those elements that match the given condition and removes the rest.
Example:
fun main() {
val numbers = mutableListOf(11, 22, 33, 44)
numbers.retainAll { it > 30 }
println(numbers)
}
Output:
[33, 44]clear() Function
If we want to remove all elements from the collection, we use the clear() function.
Example:
fun main() {
val numbers = mutableListOf(12, 23, 34, 45)
numbers.clear()
println(numbers)
}
Output:
[]
3. Updating Elements
Using the [] Operator
We can use the square brackets [] operator to change the value of an element at a specific index.
Example:
fun main() {
val items = mutableListOf("one", "two", "three")
items[2] = "two"
println(items)
}
Output:
[one, two, two]fill() Function
If we want to replace all the elements in the collection with a single value, we can use the fill() function.
Example:
fun main() {
val numbers = MutableList(4) { 0 }
numbers.fill(3)
println(numbers)
numbers.fill(0)
println(numbers)
}
Output:
[3, 3, 3, 3]
[0, 0, 0, 0]