I know this is an old question, but there's actually a new answer to it.
You're right that a hash map isn't really O(1)
, strictly speaking, because as the number of elements gets arbitrarily large, eventually you will not be able to search in constant time (and O-notation is defined in terms of numbers that can get arbitrarily large).
But it doesn't follow that the real time complexity is O(n)
--because there's no rule that says that the buckets have to be implemented as a linear list.
In fact, Java 8 implements the buckets as TreeMaps
once they exceed a threshold, which makes the actual time O(log n)
.