Initial capacity for a HashSet<Integer>
data-structures, hashmap, hashset, java
Solution
You need a `size/load-factor` to avoid a resize. Note: it will always be the next power of 2 for HashSet & HashMap.
Problem
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.