What Initial Capacity should I use for a HashSet into which I know that I am going to insert 1000 integers to prevent the need for any internal rebuilds ?
At first I though that I should use 1000 but reading the description of the constructor that taks the initialCapacity parameter it says Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
.
So If I set capacity to 1000 the hashMap will resize when reaching 750 elements?
Also I assume that some "space" is required for the effectiveness of the hashMap so solving IC*0.75=1000 to get something like 1334 might also not be the best solution or is it?
UPDATE:
1) I am aware that the implication of internal re-size is not a significant one but still its a chance to learn and better understand the environment which I am using. and the effort should be minimal.
2) Several comments where made regarding the choice of data structure. Please have a look at my previous Q here: Data structure recommendation where more exact information is provided about my scenario.
You need a size/load-factor
to avoid a resize. Note: it will always be the next power of 2 for HashSet & HashMap.
For your case, it is reasonable to set the initial capacity to 1000 and the load factor to 1 as two different Integer
s will not share the same hash (which is the int itself).
Nevertheless, for general purpose you should not really care about the load factor and leave it as it is as you will probably never notice any improvement setting it yourself. Increasing the load factor may actually lead to dramatic decrease in performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With