Tegiri said:
I suggest the O notation doesn't really work for HashMap. Consider
HashMap with lookup table size k. Then for an input n < k we indeed
have constant access time. For large input n >> k the access time
becomes linear. And the O notation is an approximation of the
complexity function when input is assumed to grow infinitely large.
Therefore, the HashMap gives you O(n) access time.
java.util.HashMap is designed to grow, I assume in order to avoid
exactly the issue you describe. You do not specify a maximum number of
buckets, as you seem to be assuming, just an initial capacity and a
maximum load factor.
When the number of entries divided by the capacity exceeds the load
factor, the capacity is approximately doubled in a rehash operation.
That keeps the lookup cost O(1), assuming good hashing.
Of course, it does affect the computational complexity of building a
HashMap with N elements. There are two interesting cases:
1. Constant initial capacity. Construction is O(1), but there will be
O(log N) rehash operations, and rehash appears to me to be linear in
current size. Insertion of an element that does not trigger a rehash is
O(1), but element insertion that does trigger a rehash is O(N). The
total build cost O(N log(N)).
2. Initial capacity linear in N. This is, of course, only practical if N
is known at construction time. Construction is O(N) but the number of
rehashes is O(1), zero given a good choice of initial capacity, and
insertion of a single element is O(1). The total build cost O(N).
Patricia