The containsKey
should be very slightly slower because it results in an extra function call (it just calls getEntry
) (it could get optimised away, I'm not sure whether Java will do so). containsKey
looks like:
public boolean containsKey(Object key) {
return getEntry(key) != null;
}
But note that containsKey
could on the other hand be very slightly faster on other Map
implementations (but probably not those in the standard Java API).
Generally my implementations look like: (avoiding the need for containsKey
)
int[] arr = map.get(100);
if (arr == null) // doesn't exist
// do stuff
else // exists
// do stuff with arr
The below would definitely be slower than the above: (if the items you look for exist a reasonable amount of the time)
if (!map.containsKey(100)) // doesn't exist
// do stuff
else // exists
{
int[] arr = map.get(100);
// do stuff with arr
}
Edit: Thanks to zvzdhk for providing the source of containsKey
. I should actually have checked.