A common pattern is to use
if(hashMap.containsKey(key)) { Object o = hashMap.get(key); }
however, if you know that none of the values ββis null (many Map collections do not allow null), you can do the following, which is more efficient.
Object o = hashMap.get(key); if (o != null) { }
BTW: containsKey is the same as
Set<Key> keys = hashMap.keySet(); boolean containsKey = keys.contains(key);
source share