Kotlin

Collection Write Operations

Swift

Adding elements

                  val numbers = mutableListOf(1, 2, 3, 4)
numbers.add(5)
println(numbers)
                
                    let numbers = mutable[1, 2, 3, 4]
numbers.add(5)
print(numbers)
                  
                  val numbers = mutableListOf(1, 2, 5, 6)
numbers.addAll(arrayOf(7, 8))
println(numbers)
numbers.addAll(2, setOf(3, 4))
println(numbers)
                
                    let numbers = mutableListOf(1, 2, 5, 6)
numbers.addAll(arrayOf(7, 8))
print(numbers)
numbers.addAll(2, setOf(3, 4))
print(numbers)
                  
                  val numbers = mutableListOf("one", "two")
numbers += "three"
println(numbers)
numbers += listOf("four", "five")    
println(numbers)
                
                    let numbers = mutableListOf("one", "two")
numbers += "three"
print(numbers)
numbers += listOf("four", "five")    
print(numbers)
                  

Removing elements

                  val numbers = mutableListOf(1, 2, 3, 4, 3)
numbers.remove(3)                    // removes the first `3`
println(numbers)
numbers.remove(5)                    // removes nothing
println(numbers)
                
                    let numbers = mutableListOf(1, 2, 3, 4, 3)
numbers.remove(3)                    // removes the first `3`
print(numbers)
numbers.remove(5)                    // removes nothing
print(numbers)
                  
                  val numbers = mutableListOf(1, 2, 3, 4)
println(numbers)
numbers.retainAll { it >= 3 }
println(numbers)
numbers.clear()
println(numbers)
​
val numbersSet = mutableSetOf("one", "two", "three", "four")
numbersSet.removeAll(setOf("one", "two"))
println(numbersSet)
                
                    let numbers = mutable[1, 2, 3, 4]
print(numbers)
numbers.retainAll { $0 >= 3 }
print(numbers)
numbers.clear()
print(numbers)
​
let numbersSet = mutableSetOf("one", "two", "three", "four")
numbersSet.removeAll(setOf("one", "two"))
print(numbersSet)
                  
                  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)
                
                    let numbers = mutableListOf("one", "two", "three", "three", "four")
numbers -= "three"
print(numbers)
numbers -= listOf("four", "five")    
//numbers -= listOf("four")    // does the same as above
print(numbers)