You won't gain anything by checking that the key exists. This is the code of HashMap:
@Override public boolean containsKey(Object key) { Entry<K, V> m = getEntry(key); return m != null; } @Override public V get(Object key) { Entry<K, V> m = getEntry(key); if (m != null) { return m.value; } return null; } Just check if the return value for get() is different from null.
This is the HashMap source code.
Resources :
- HashMap source code
HashMap source codeBad one - HashMap source code Good one