how do I check if a map contains a certain entry?
# announcements
f
how do I check if a map contains a certain entry?
c
Same as in java.
yourMap.containsKey("someKey")
By value you have to check all the values, e.g.
yourMap.find { it.value == yourValue } != null
k
I'd do
key in map
and
value in map.values
.
K 1
👍 4
I was about to say the performance of
map.values
can be bad, but
HashMap
caches the result (and it even updates with the map), so it should be fine.
f
thanks
@karelpeeters but what if the map contains both key and value and at the same time the target value is stored by some another key?
k
Ah you're asking whether a map contains a given entry, I think we all read over that 🙂.
What is wrong with
map[key] == value
then?
f
well, looks like nothing is wrong, haha
thanks 🙂