Test has-key before get
Updated
When the miss sentinel can be a real value (0, −1, null), has-key then get is the safe pair. Languages with get-throwing or optional-get make the same idea explicit.
What to focus on
has-key + get is two hashes. getOrDefault is one. Prefer one lookup if the API gives you an optional. Never treat got 0 as absent if 0 can be stored.
Worked examples
safe present
a = [4] as {4:0}, has 4 then get- has true
- value 0 is data
Result: use 0
safe absent
a = [4] as {4:0}, has 1 then skip get- has false
Result: do not call get as if 0
empty
a = [], has 4- false
Result: skip get
Try it
Write hasKey(m, k) then get(m, k). m is the hash map. k is the key. 0 is a legal stored value. Branch on hasKey. Do not interpret 0 as miss. a = [4] as {4:0}, has 4 then get → use 0. a = [4] as {4:0}, has 1 then skip get → do not call get as if 0. a = [], has 4 → skip get.
keys are integers. 0 is a legal stored value. Empty is in the cases.