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.
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.
val set = setOf(1, 2, 3, 4, 5)
println(set + 6)
println(set + setOf(6, 7, 8))
println(set - 5)
println(set - setOf(5, 6))
Plus and Minus with Map
Plus and minus operators work a bit different in the case of map.
To plus a single element to the existing map, we can pass it as a Pair.
If the item is already there in the map, the plus operator will override its value for that particular key.
We can even pass another map to add the key-value pairs. if any of the keys already exist in the map, this will override its value.
In the case of minus, since all the keys are unique, it will just remove the key-value pairs from the existing ones.
val map = mapOf("key1" to 1, "key2" to 2, "key3" to 3)
println(map + Pair("key4", 4))
println(map + Pair("key1", 100))
println(map + mapOf("key5" to 5, "key1" to 100))
println(map - "key1")
println(map - listOf("key1", "key2"))