From the JavaDocs of HashSet:
This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the ha
Why does iteration takes time proportional to the sum(number of elements in set+ capacity of backing map) and not only to the number of elements in the set itself ?
The elements are dispersed inside the underlying HashMap which is backed by an array.
So it is not known which buckets are occupied (but it is known how many elements are totally available).
So to iterate over all elements all buckets must be checked