How can a HashSet offer constant time add operation?

前端 未结 2 378
失恋的感觉
失恋的感觉 2020-12-18 23:03

I was reading the javadocs on HashSet when I came across the interesting statement:

This class offers constant time performance for the basic operatio

2条回答
  •  梦毁少年i
    2020-12-18 23:32

    The number of buckets is dynamic, and is approximately ~2n, where n is the number of elements in the set.

    Note that HashSet gives amortized and average time performance of O(1), not worst case. This means, we can suffer an O(n) operation from time to time.
    So, when the bins are too packed up, we just create a new, bigger array, and copy the elements to it.
    This costs n operations, and is done when number of elements in the set exceeds 2n/2=n, so it means, the average cost of this operation is bounded by n/n=1, which is a constant.

    Additionally, the number of collisions a HashMap offers is also constant on average.

    Assume you are adding an element x. The probability of h(x) to be filled up with one element is ~n/2n = 1/2. The probability of it being filled up with 3 elements, is ~(n/2n)^2 = 1/4 (for large values of n), and so on and so on.
    This gives you an average running time of 1 + 1/2 + 1/4 + 1/8 + .... Since this sum converges to 2, it means this operation takes constant time on average.

提交回复
热议问题