.remove()
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}
Contribute to Docs
- 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.
Learn Kotlin on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Kotlin
Learn Kotlin, the expressive, open-source programming language developed by JetBrains.Beginner Friendly9 hours