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]
Similar Reads
Kotlin - Collection Operations Overview
The Kotlin standard library provides a wide range of functions for performing operations on collections. It includes simple operations like getting or adding elements and also includes more complex ones like searching, sorting, filtering, etc. Member and Extension functions - Collection operations a
4 min read
Kotlin | Retrieve Collection Parts
The slice function allows you to extract a list of elements from a collection by specifying the indices of the elements you want to retrieve. The resulting list contains only the elements at the specified indices. The subList function allows you to retrieve a sublist of a collection by specifying th
5 min read
Kotlin | Collection Transformation
Kotlin standard library provides different set of extension functions for collection transformations. These functions help in building new collections from existing collections based on the rules defined by the transformation. There are different number of transformation functions: MappingZippingAss
5 min read
Kotlin Collections Ordering
There are certain collections where the order of the elements is important. If you take lists, two lists are never equal if they are ordered differently inspite of having same elements.In Kotlin, there are different ways of ordering the elements. Most built-in types are comparable: -Numeric types fo
4 min read
Merge Two Collections in Kotlin
Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
4 min read
Kotlin Collections
In Kotlin, collections are used to store and manipulate groups of objects or data. There are several types of collections available in Kotlin, including: Lists - Ordered collections of elements that allow duplicates.Sets - Unordered collections of unique elements.Maps - Collections of key-value pair
6 min read
Kotlin Elvis Operator(?:)
Elvis Operator (?:)It is used to return the not null value even the conditional expression is null. It is also used to check the null safety of values. In some cases, we can declare a variable which can hold a null reference. If a variable st which contains null reference, before using st in program
2 min read
What is a collection in MongoDB?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for the storage and retrieval of data. Thi
4 min read
Kotlin | Filtering Collections
In Kotlin, filtering is a prominent task of collection processing. The filtering conditions are defined by predicates â lambda functions that take a collection element and return true when the given element matches the predicate, and false means it doesn't match the predicate. There are standard lib
4 min read
Kotlin partition() Method with Examples
In this article, we are going to discuss how to split the original collection into pair of collections, because sometimes while coding, you wish that you could just split a list into sublists without going into the for and while loops. Kotlin provides you with a function just for this occasion. In t
4 min read