Open In App

Kotlin Collection Write operations

Last Updated : 22 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Adding elements
  2. Removing elements and
  3. 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 –




//add a single element to a list or
//a set, use the add() function
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 –




//add multiple elements to a list
//or a set, use the addAll() function
fun main(args: Array<String>) {
    val numbers = mutableListOf(1, 2, 5, 6)
    // add set elements to list
    numbers.addAll(2, setOf(2, 13, 14))
    println(numbers)
  
    // add array elements to list
    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 –




//add element to a list
//or a set, use the plus operator
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 –




//remove element to a list
//or a set, use the remove() function
fun main(args: Array<String>) {
    val numbers = mutableListOf(11, 22, 33, 44, 33)
    // removes first occurrence 33
    numbers.remove(33)
    println(numbers)
    // removes nothing
    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 –




//remove element to a list
//or a set, use the removeAll() function
fun main(args: Array<String>) {
    val counting = mutableSetOf("one", "two", "three", "four")
    println(counting)
    // remove all the elements passed as an argument
    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 –




//remove element to a list or a set
fun main(args: Array<String>) {
    val counting = mutableListOf("one", "two", "three", "three", "four")
    counting -= "three"
    println(counting)
    // removes the elements
    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 –




//remove element to a list or a set, use the retainAll() function
fun main(args: Array<String>) {
    val numbers = mutableListOf(11, 22, 33, 44)
    println(numbers)
    // only retain the numbers which satisfy the condition
    numbers.retainAll { it >= 33 }
    println(numbers)
}


Output:

[11, 22, 33, 44]
[33, 44]

 
clear() function delete all the collection elements.
Kotlin program –




//remove element to a list or a set, use the clear() function
fun main(args: Array<String>) {
    val numbers = mutableListOf(12, 23, 34, 45)
    println(numbers)
    // it empty the list by removing all
    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 –




//update element to a list 
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 –




//update element to a list
fun main(args: Array<String>) {
    val numbers = mutableListOf(1, 2, 3, 4)
    // replaces all elements by 3
    numbers.fill(3)
    println(numbers)
    // replaces all elements by 0
    numbers.fill(0)
    println(numbers)
}


Output:

[3, 3, 3, 3]
[0, 0, 0, 0]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads