https://kotlinlang.org logo
Title
f

forcelain

11/26/2017, 11:05 AM
how do I check if a map contains a certain entry?
c

Czar

11/26/2017, 11:11 AM
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

karelpeeters

11/26/2017, 11:20 AM
I'd do
key in map
and
value in map.values
.
👍 4
:kotlin: 1
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

forcelain

11/26/2017, 12:06 PM
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

karelpeeters

11/26/2017, 12:10 PM
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

forcelain

11/26/2017, 12:13 PM
well, looks like nothing is wrong, haha
thanks 🙂