Is checking for key existence in HashMap always necessary?
I have a HashMap with say a 1000 entries and I am looking at improving the efficiency. If the HashMap is b
You won't gain anything by checking that the key exists. This is the code of HashMap:
@Override
public boolean containsKey(Object key) {
Entry m = getEntry(key);
return m != null;
}
@Override
public V get(Object key) {
Entry 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 :