It depends on many things. It’s *usually* O(1), with a decent hash which itself is constant time… but you could have a hash which takes a long time to compute, *and* if there are multiple items in the hash map which return the same hash code, `get`

will have to iterate over them calling `equals`

on each of them to find a match.

In the worst case, a `HashMap`

has an O(n) lookup due to walking through all entries in the same hash bucket (e.g. if they all have the same hash code). Fortunately, that worst case scenario doesn’t come up very often in real life, in my experience. So no, O(1) certainly isn’t guaranteed – but it’s usually what you should assume when considering which algorithms and data structures to use.

In JDK 8, `HashMap`

has been tweaked so that if keys can be compared for ordering, then any densely-populated bucket is implemented as a tree, so that even if there are lots of entries with the same hash code, the complexity is O(log n). That can cause issues if you have a key type where equality and ordering are different, of course.

And yes, if you don’t have enough memory for the hash map, you’ll be in trouble… but that’s going to be true whatever data structure you use.