.remove()

dakshdeepHERE23 total contributions
Published Feb 7, 2023
Contribute to Docs
The .remove()
method is used to remove a key-value pair from a HashMap
in Kotlin.
Syntax
fun <K, V> MutableMap<K, V>.remove(key: K): V?
It takes one parameter and returns:
key
: the key of the key-value pair to be removedV?
: the value associated with the removed key or null if the key is not found in the map.
Example
Here is an example of how to use the .remove()
method in a Kotlin program:
fun main(){val myMap = hashMapOf("key1" to "value1", "key2" to "value2")val removedValue = myMap.remove("key1")print(myMap)}
The output for the above code will be:
{key2=value2}
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.