Published Feb 7, 2023
Contribute to Docs

The .remove() method is used to remove a key-value pair from a HashMap in Kotlin.


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 removed
  • V? : the value associated with the removed key or null if the key is not found in the map.


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")

The output for the above code will be:


All contributors

Looking to contribute?

Learn Kotlin on Codecademy