Kotlin Collection Write operations

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 –



filter_none

edit
close

play_arrow

link
brightness_4
code

//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)
}

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

//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)
}

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

//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)
}

chevron_right


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 occurance of the element. If an element does not exist in the list then removes nothing.
Kotlin program to demonstrate the removal of element –

filter_none

edit
close

play_arrow

link
brightness_4
code

//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)
}

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

//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)
}

chevron_right


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 –


filter_none

edit
close

play_arrow

link
brightness_4
code

//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)
}

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

//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)
}

chevron_right


Output:

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

//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)
}

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

//update element to a list 
fun main(args: Array<String>) {
    val numbers = mutableListOf("one", "five", "three")
    numbers[1] =  "two"
    numbers[2] =  "two"
    println(numbers)
}

chevron_right


Output:

[one, two, two]

 
fill() function is used to replaces all the collection elements with the specified value..
Kotlin program –

filter_none

edit
close

play_arrow

link
brightness_4
code

//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)
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.