so my question is:
why hashMap requires a power of 2 as initial capacity? and while hashtable choose 11 as the default initial capacity?
i assume this has nothing to do with the thing that hashtable is thread safe and does not allow null key or values.

4 Answers
4

According to that article, the main reason to move to power-of-two sizes was that bit masking is faster than integer division. This is not without adverse consequences, which are explained by one of the original authors:

Joshua Bloch: The downside of using a power-of-two is that the resulting hash table is very sensitive to the quality of the hash function (hashCode). It is imperative that any change in the input must affect the low order bits of the hash value. (Ideally, it should affect all bits of the hash value with equal likelihood.) Because we have no assurance that this is true, we put in a secondary (or "defensive") hash function when we switched to the power-of-two hash table. This hash function is applied to the results of hashCode before masking off the low order bits. Its job is to scatter the information over all the bits, and in particular, into the low order bits. Of course it has to run very fast, or you lose the benefit of switching to the power-of-two-sized table. The original secondary hash function in 1.4 turned out to be insufficient. We knew that this was a theoretical possibility, but we thought that it didn't affect any practical data sets. We were wrong. The replacement secondary hash function (which I developed with the aid of a computer) has strong statistical properties that pretty much guarantee good bucket distribution.

"Your answer is helpful, but you can make it better by including a summary or relevant portions of the pages you're linking to. This will also help your answer remain great even if the links you included break in the future." - meta.stackexchange.com/questions/92505/…
–
ArjunShankarFeb 23 '12 at 13:37

Hashtable uses pseudo-prime number table sizes and grows the size of the table relatively slower. HashMap uses a power of 2 as the bit wise and is faster than using modulus.

Ironically, a modulus of a power of 2 means a good hashCode() is needed as the top bits would be ignored so HashMap has a method to rearrange the hashCode you get to avoid this issue meaning it can actually be slower. :Z

How does this help? Would be nice you you could explain what you mean here. That link could break some day, and people visiting this page for an answer will have learned nothing.
–
ArjunShankarFeb 23 '12 at 13:42

Done, ArjunShankar. I saw your comment in a previous answer and I thought to do the same in my answer. :)
–
gregurenFeb 23 '12 at 13:45

The requirement for the table size to be a power of two is an implementation detail, not known to the users of the class -- that is why the c'tor silently adjusts the value to the next larger power of two instead of flagging an error.

The Hashtable implementation assumes that the hash may not be evenly distributed, so it tries to use a number of bins that is prime in the hope of avoiding peaks in the frequency distribution of the hash.

The combination of these two implementation details leads to bad performance.

(e.g. a primitive hash function would be

int hash(String s, int nBins) {
return s[0] % nBins;
}

If nBins is 32, e and E end up in the same bin, so the distribution of hash values correlates with the distribution of occurence of letters, which has distinct peaks -- so the frequency distribution would have a peak at 32.)