Plus(+) and Minus(-) Operator : Kotlin
The collection is something which is used by almost everyone. It makes our life easy. List, Set and Map are the best examples of them.
To iterate, filter or modify the existing collection object, Kotlin provides us with a few in builds transform operators. Plus and Minus operator are few of them of them.
Lets try to understand the plus and minus operator in detail.
This post was originally posted at https://agrawalsuneet.github.io/blogs/plus-and-minus-operator-kotlin/ and later reposted on Medium.
What are plus and minus operator?
Plus operator is used to creating a new collection object by adding an element or a list of elements to the existing one.
Keep in mind that the returned collection object is read-only or val type.
The values that need to be added, can be passed as single element or a list or collection of elements.
The type alias for plus function is +
Minus operator is used to creating a new collection object by removing an element or a list of elements to the existing one.
Keep in mind that the returned collection object is read only or val type.
The values that need to be removed, can be passed as single element or a list or collection of elements.
But, in case of minus, if the item which is subtracted is just a single element, it will just remove its first occurrence but if we remove by passing it in a collection, it will remove all the occurrence of that element.
The type alias for minus function is -
/**
* Returns a list containing all elements of the original collection and then all elements of the given [elements] array.
*/
public operator fun <T> Collection<T>.plus(elements: Array<out T>): List<T> {
val result = ArrayList<T>(this.size + elements.size)
result.addAll(this)
result.addAll(elements)
return result
}
/**
* Returns a list containing all elements of the original collection and then all elements of the given [elements] collection.
*/
public operator fun <T> Collection<T>.plus(elements: Iterable<T>): List<T> {
if (elements is Collection) {
val result = ArrayList<T>(this.size + elements.size)
result.addAll(this)
result.addAll(elements)
return result
} else {
val result = ArrayList<T>(this)
result.addAll(elements)
return result
}
}
/**
* Returns a list containing all elements of the original collection without the first occurrence of the given [element].
*/
public operator fun <T> Iterable<T>.minus(element: T): List<T> {
val result = ArrayList<T>(collectionSizeOrDefault(10))
var removed = false
return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true }
}
/**
* Returns a list containing all elements of the original collection except the elements contained in the given [elements] array.
*/
public operator fun <T> Iterable<T>.minus(elements: Array<out T>): List<T> {
if (elements.isEmpty()) return this.toList()
val other = elements.convertToSetForSetOperation()
return this.filterNot { it in other }
}
Plus and Minus with List
Plus operator adds the elements to the existing list and returns a read-only (val type) list.
Minus operator removes the element from the existing list and returns a read-only (val-type) list. If we pass a single element, it will only remove its first occurrence but if we pass it as a list, it will remove all the occurrences of the second list elements from the first list.
val list = listOf(1, 2, 3, 4, 5, 5)
println(list + 6)
println(list + listOf(6, 7, 8))
println(list - 5)
println(list - listOf(5, 6))
Plus and Minus with Set
Plus operator adds the elements to the existing set and returns a read-only (val type) set. If the item already exists in the set, it will not change anything but will just convert it to a read-only set.
Minus operator removes the element from the existing set and returns a read-only (val-type) set. Since all the elements in a set are unique, it doesn’t matter if we pass it as a single element or as a set, it will remove them from the existing set.