0

HashSet(int initialCapacity)

  • Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).

But the term "initial capacity" is never disclosed.

Some sources say it is the actual number of buckets, so then it is advisable to store not more than capacity * loadFactor elements.

I think "initial capacity" argument is the number of elements I can store (optimally), so the physical number of buckets will be elements / loadFactor, then rounded towards the power of two (away from zero).

caasdads
  • 409
  • 4
  • 12
  • 4
    Second line of the javadoc of HashSet: *the "capacity" of the backing HashMap instance (the number of buckets). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.* Third line of the javadoc of HashMap: *An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created.* – JB Nizet Dec 07 '18 at 12:41
  • I noticed that, but Javadoc encloses the word capacity in quotes here, so the ambiguity is never resolved - the word capacity as an argument and in this place can mean two different things. So alas, I cannot rely on that javadoc... – caasdads Dec 07 '18 at 12:43
  • 2
    You can, of course. They don't mean different things. The documentation of HashMap (which the javadoc clearly says a HashSet is backed by) also has the definition. See my amended first comment. – JB Nizet Dec 07 '18 at 12:44
  • 1
    It's obviously the number of buckets. How did you understand something else? – fps Dec 07 '18 at 13:05

0 Answers0