Kotlin Collection Write operations
Last Updated :
22 Nov, 2019
Collection Write operations are used to change the content of MutableCollection. MutableCollection is defined as the Collection with write operations like add and remove.
Operations supported are as follows:
- Adding elements
- Removing elements and
- Updating elements
Addition of elements –
add() function is used to add element to the existing group of mutable collection. It appends the object to the end of the collection.
Kotlin program to demonstrate the addition of an element –
fun main(args: Array<String>) {
val integers = mutableListOf(11, 12, 13, 14)
integers.add(15)
println(integers)
}
|
Output:
[11, 12, 13, 14, 15]
addAll() function is used for adding all the elements of a list to the mutable collection. The argument can be of any form an Iterable, a Sequence, or an Array.
Kotlin program to demonstrate the addition of elements –
fun main(args: Array<String>) {
val numbers = mutableListOf(1, 2, 5, 6)
numbers.addAll(2, setOf(2, 13, 14))
println(numbers)
numbers.addAll(arrayOf(7, 8))
println(numbers)
}
|
Output:
[1, 2, 2, 13, 14, 5, 6]
[1, 2, 2, 13, 14, 5, 6, 7, 8]
plusAssign (+=) operator is also used to add element in the collection.
Kotlin program to demonstrate the addition of elements –
fun main(args: Array<String>) {
val list = mutableListOf( "one" , "two" , "seven" )
list += listOf( "four" , "five" )
println(list)
list += "three"
println(list)
}
|
Output:
[one, two, seven, four, five]
[one, two, seven, four, five, three]
Removal of the elements –
remove() function is used to delete a element from a mutable collection. It removes the first occurrence of the element. If an element does not exist in the list then removes nothing.
Kotlin program to demonstrate the removal of element –
fun main(args: Array<String>) {
val numbers = mutableListOf(11, 22, 33, 44, 33)
numbers. remove (33)
println(numbers)
numbers. remove (53)
println(numbers)
}
|
Output:
[11, 22, 44, 33]
[11, 22, 44, 33]
removeAll() function is used to delete all elements that are matched with arguments.
Kotlin program to demonstrate the removal of element –
fun main(args: Array<String>) {
val counting = mutableSetOf( "one" , "two" , "three" , "four" )
println(counting)
counting.removeAll(setOf( "one" , "two" , "four" ))
println(counting)
}
|
Output:
[one, two, three, four]
[three]
minusAssign (- =) operator is also used to remove elements in the collection.
Kotlin program to demonstrate the removal of element –
fun main(args: Array<String>) {
val counting = mutableListOf( "one" , "two" , "three" , "three" , "four" )
counting -= "three"
println(counting)
counting -= listOf( "four" , "five" , "two" )
println(counting)
}
|
Output:
[one, two, three, four]
[one, three]
retainAll() function is also used to remove elements. It remove elements are not satisfied by the condition.
Kotlin program –
fun main(args: Array<String>) {
val numbers = mutableListOf(11, 22, 33, 44)
println(numbers)
numbers.retainAll { it >= 33 }
println(numbers)
}
|
Output:
[11, 22, 33, 44]
[33, 44]
clear() function delete all the collection elements.
Kotlin program –
fun main(args: Array<String>) {
val numbers = mutableListOf(12, 23, 34, 45)
println(numbers)
numbers.clear()
println(numbers)
}
|
Output:
[12, 23, 34, 45]
[]
Updation of the elements –
Updating elements operation is also provide by Mutable collection.
operator [] is used to replace an element at a given position.
Kotlin program –
fun main(args: Array<String>) {
val numbers = mutableListOf( "one" , "five" , "three" )
numbers[1] = "two"
numbers[2] = "two"
println(numbers)
}
|
Output:
[one, two, two]
fill() function is used to replaces all the collection elements with the specified value..
Kotlin program –
fun main(args: Array<String>) {
val numbers = mutableListOf(1, 2, 3, 4)
numbers.fill(3)
println(numbers)
numbers.fill(0)
println(numbers)
}
|
Output:
[3, 3, 3, 3]
[0, 0, 0, 0]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...