What is the time complexity of HashMap.containsKey() in java?

Hossein picture Hossein · Jan 19, 2012 · Viewed 62.1k times · Source

I need to know: What is the time complexity of HashMap.containsKey() in java?

Answer

Michael Borgwardt picture Michael Borgwardt · Jan 19, 2012

From the API doc ofHashMap:

This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets.

Since containsKey() is just a get() that throws away the retrieved value, it's O(1) (assuming the hash function works properly, again).