Issue
I have a map as follows
val parentMap= mutableMapOf<String,String>()
parentMap["key1"]="value1"
parentMap["key2"]="value2"
parentMap["key3"]="value3"
And I have a list of keys val keyList= listOf("key1","key3")
I want to remove the key
from map which doesn't exist in my keylist
The current solution I am using is
val filteredMap= mutableMapOf<String,String>()
keyList.forEach {
if(parentMap.containsKey(it))
filteredMap[it]=parentMap[it]!!
}
println(filteredMap)
Is there any more better way to do this?
Solution
You can do it much easier like this:
val filteredMap = parentMap.filter { keyList.contains(it.key) }
Answered By - Ivo Beckers
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.