Reputation: 1661
The Java Hashtable has a constructor where you can specify a loadFactor. However, if the initialCapacity (n) is known, what's the point of specifying the loadFactor?
Assuming that the size of its array of buckets (s) is constant, does the constructor Hashtable(int initialCapacity, float loadFactor)
just create a Hash Table that has more capacity than initialCapacity to ensure the correct loadFactor?
Upvotes: 5
Views: 1079
Reputation: 183564
Assuming that the size of its array of buckets (s) is constant, […]
This assumption is not correct. The array of buckets is resized when necessary to ensure that the proportion of non-empty buckets is at most loadFactor
.
(Note: the Javadoc states that "The initial capacity and load factor parameters are merely hints to the implementation. The exact details as to when and whether the rehash method is invoked are implementation-dependent", so the above shouldn't be taken as a strict guarantee. But it's the general behavior.)
Upvotes: 6
Reputation: 6003
If you know exactly the size of your table, then make the loadfactor 1. The class is written for everyone -- including the likely case that people will want to increase the size of the array.
Upvotes: -1
Reputation: 76918
Because it's only the initial capacity. The HashMap
is a dynamic structure; you can exceed the initial capacity which is what the loadfactor is used for - to know when to expand.
(And the data structure has no way of divining your intentions if you mean to say you know that you're never going to exceed that initial capacity; it's a data structure, not a clairvoyant AI ;) ).
Upvotes: 2