Remove mapping for a key from a Map in Kotlin
This article explores different ways to remove the mapping for a key from a Map in Kotlin.
1. Using remove() function
The standard solution is to remove a key from a Map in Kotlin is using the remove() function. When the remove() function is called upon a key k, the mapping from key k to value v is removed from the map.
|
1 2 3 4 5 |
fun main() { val lang = mutableMapOf(Pair("C++", 1980), Pair("Kotlin", 2011), Pair("Ruby", 1995)) lang.remove("C++") println(lang) // {Kotlin=2011, Ruby=1995} } |
To remove multiple keys from the map, a better solution is to use the removeAll() function:
|
1 2 3 4 5 |
fun main() { val lang = mutableMapOf(Pair("C++", 1980), Pair("Kotlin", 2011), Pair("Ruby", 1995)) lang.keys.removeAll(listOf("C++", "Kotlin")) println(lang) // {Ruby=1995} } |
2. Using removeIf() function
Another option is to use the removeIf() function to remove all mappings from the map that satisfy the specified predicate. It can be used as follows:
|
1 2 3 4 5 |
fun main() { val lang = mutableMapOf(Pair("C++", 1980), Pair("Kotlin", 2011), Pair("Ruby", 1995)) lang.entries.removeIf { it.key == "C++" } println(lang) // {Kotlin=2011, Ruby=1995} } |
The following code example shows invocation for this function on the mutable set of all keys present in the map.
|
1 2 3 4 5 |
fun main() { val lang = mutableMapOf(Pair("C++", 1980), Pair("Kotlin", 2011), Pair("Ruby", 1995)) lang.keys.removeIf { it == "C++" } println(lang) // {Kotlin=2011, Ruby=1995} } |
3. Using filterKeys() function
All the above solutions remove the key from the original map. If you need a new instance of the map with the specified key removed, use the filterKeys() function.
|
1 2 3 4 5 6 7 |
fun main() { val lang = mutableMapOf(Pair("C++", 1980), Pair("Kotlin", 2011), Pair("Ruby", 1995)) val filteredMap = lang.filterKeys { it != "C++" } println(filteredMap) // {Kotlin=2011, Ruby=1995} } |
If you need to filter the map based on values, use the filterValues() function:
|
1 2 3 4 5 6 7 |
fun main() { val lang = mutableMapOf(Pair("C++", 1980), Pair("Kotlin", 2011), Pair("Ruby", 1995)) val filteredMap = lang.filterValues { it != 1980 } println(filteredMap) // {Kotlin=2011, Ruby=1995} } |
4. Using mapNotNull() function
The mapNotNull() function returns a list containing only the non-null results of applying the given transform function to each entry in the original map. It can be used as follows to remove, replace, or retain a mapping based on a key:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun main() { val lang = mutableMapOf(Pair("C++", 1980), Pair("Kotlin", 2011), Pair("Ruby", 2000)) val filteredMap = lang.mapNotNull { (key, value) -> when (key) { "C++" -> null // remove "Kotlin" -> "Kotlin by JetBrains" to value // replace else -> key to value // retain } }.toMap() println(filteredMap) // {Kotlin by JetBrains=2011, Ruby=2000} } |
Alternatively, remove, replace, or retain a mapping based on a value as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun main() { val lang = mutableMapOf(Pair("C++", 1980), Pair("Kotlin", 2011), Pair("Ruby", 2000)) val filteredMap = lang.mapNotNull { (key, value) -> when (value) { 1980 -> null // remove 2011 -> key to 2021 // replace else -> key to value // retain } }.toMap() println(filteredMap) // {Kotlin=2021, Ruby=2000} } |
Also See:
That’s all about removing mapping for a key from a Map in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)