Initial Capacity for HashSet <Integer>
What initial capacity should I use for a HashSet in which I know I'm going to insert 1000 integers to prevent the need for any internal overhauls?
At first I, although I have to use 1000, but after reading the description of the constructor that introduces 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). Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75). .
So, if I set the capacity to 1000, will hashMap resize when it reaches 750 elements?
I also assume that hashMap requires some โspaceโ for efficiency, so the IC * 0.75 = 1000 solution, to get something like 1334, also cannot be a better solution or is it?
UPDATE:
1) I know that the consequences of internal oversizing are not significant, but there is still a chance to learn and better understand the environment that I use. and efforts should be minimal.
2) A few comments regarding the choice of data structure. Please see my previous Q here: Data structure recommendation for more details on my scenario.
If it really is worth worrying about this (and I suspect it is not: resizing a set of 1000 integers will not take much time), then keep in mind that the HashSet supported by the HashMap and put this method:
addEntry(int hash, K key, V value, int bucketIndex) { Entry<K,V> e = table[bucketIndex]; table[bucketIndex] = new Entry<K,V>(hash, key, value, e); if (size++ >= threshold) resize(2 * table.length); } It is always worth checking the source cod for such requests, although remember that the implementation can always change (even for small releases of JRE).
Finally, is this a kit suitable for this scenario? If you have a fixed integer distribution size, maybe a simple array (using primitives and thus avoiding boxing) will be faster / easier?
In your case, it is wise to set the initial capacity to 1000 and the load factor equal to 1, since two different Integer will not use the same hash (which is int itself).
However, for general purposes, you donโt need to worry about the load factor and leave it as it is, since you will probably never notice any improvements installing it yourself. Increasing the load factor can lead to a significant decrease in performance.