Collection Write Operations | Kotlin
Maybe your like
Removing elements
To remove an element from a mutable collection, use the remove() function. remove() accepts the element value and removes one occurrence of this value.
fun main() { //sampleStart val numbers = mutableListOf(1, 2, 3, 4, 3) numbers.remove(3) // removes the first `3` println(numbers) numbers.remove(5) // removes nothing println(numbers) //sampleEnd }For removing multiple elements at once, there are the following functions :
removeAll() removes all elements that are present in the argument collection. Alternatively, you can call it with a predicate as an argument; in this case the function removes all elements for which the predicate yields true.
retainAll() is the opposite of removeAll(): it removes all elements except the ones from the argument collection. When used with a predicate, it leaves only elements that match it.
clear() removes all elements from a list and leaves it empty.
Another way to remove elements from a collection is with the minusAssign (-=) operator – the in-place version of minus. The second argument can be a single instance of the element type or another collection. With a single element on the right-hand side, -= removes the first occurrence of it. In turn, if it's a collection, all occurrences of its elements are removed. For example, if a list contains duplicate elements, they are removed at once. The second operand can contain elements that are not present in the collection. Such elements don't affect the operation execution.
fun main() { //sampleStart val numbers = mutableListOf("one", "two", "three", "three", "four") numbers -= "three" println(numbers) numbers -= listOf("four", "five") //numbers -= listOf("four") // does the same as above println(numbers) //sampleEnd }Tag » Add Element List Kotlin
-
Ajouter Des éléments à Une Liste Dans Kotlin - Techie Delight
-
How To Add An Item To A List In Kotlin? - Stack Overflow
-
How To Add Element To List In Kotlin? - Tutorial Kart
-
How To Add Element To List At Specific Index In Kotlin? - Tutorial Kart
-
6 Ways To Add Items To A List In Kotlin - CodeVsColor
-
How To Add An Item To A List In Kotlin? - Tutorialspoint
-
Working With Lists In Kotlin - Baeldung
-
Kotlin Lists - Working With Lists In Kotlin - ZetCode
-
Add Element In Kotlin List Code Example - Code Grepper
-
Kotlin List & Mutable List Tutorial With Examples - BezKoder
-
Add Items To A List In Kotlin | Delft Stack
-
Shooting Yourself In The Foot While Adding An Element To A Kotlin List
-
Kotlin List : ListOf() - GeeksforGeeks
-
Kotlin Lists (listOf & MutableListOf): Explained With 16 Examples
-
Data Structures & Algorithms In Kotlin, Chapter 3: Linked List
-
Kotlin ArrayList - Javatpoint
-
How Mutable List Works In Kotlin With Examples? - EduCBA
-
What Is The dAll() Method In Kotlin?
-
How To Add New Element In Array In Kotlin ? - Tutorialwing