This should be an easy one. I need to create a HashMap with a known
size. I.e. I know in advance exactly how many entries the map will
contain, and that number will never grow.
Is the following appropriate?
Map myMap = new HashMap(mapSize, 1);
Not really. For a HashMap to work well, you want it to be about 25%
larger than the number of things it must contain. As you squeeze it,
the number of collisions (two objects hashing to the same slot) goes
up drastically and the average chain length to search goes up.
Hashtables work fastest if the capacity is a prime number since it
reduces collisions. Your hashcodes are unlikely to have a frequency
distribution pattern based on modulo a large prime number, where they
easily could have one based on a power of two. When you use a power of
two, you are effectively masking off all but the low order bits. In
Java 1.4.1+, for HashMap there no longer a payback for specifying
prime table lengths because HashMap rounds whatever capacity you
specify up to a power of two, to avoid the cost of a division (they
can do an AND bit mask instead) and to help keep memory from being
fragmented. However, Hashtable, the synchronized version, uses the
value you give it, so giving it a prime is still worth while.
Now it is more important than ever to have a high quality hashCode
function that has good distribution, especially in the low order bits.
It must not favour one bit pattern over another, e.g. not tend to
generate hashcodes all ending in binary 0000. In JDK 1.4.1+ there in a
rehash function used inside HashMap to improve the quality of your
provided hashCode by redistributing the bits. rehash has to be quick,
or they might as well have used tables whose lengths are multiples of
primes and used the modulus operator.
See
http://mindprod.com/jgloss/hashtable.html
http://mindprod.com/jgloss/hashmap.html