Why is the initialCapacity of Hashtable 11 while the DEFAULT_INITIAL_CAPACITY in HashMap is 16 and requires a power of 2?

10,431

Solution 1

The following article addresses this question in some detail: HashMap requires a better hashCode() - JDK 1.4 Part II.

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.

Solution 2

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

Solution 3

This could help:

http://www.concentric.net/~Ttwang/tech/primehash.htm

Basicly, if I remember correctly, when you have a hash table with a size that is power of 2, it's easy to get a hash function based on the less relevant bits of the key.

Using a prime number (as in 11) as the size of the table, makes collision on the table rows less likely, so inserting is "cheaper".

Share:
10,431
hetaoblog
Author by

hetaoblog

http://www.chinatech.us, lots of chinese tech, stock and company news http://www.hetaoblog.com, this is my chinese tech blog

Updated on June 03, 2022

Comments

  • hetaoblog
    hetaoblog about 2 years

    Comparing the HashMap and Hashtable source code in JDK 1.6, I saw the below code inside HashMap:

    /**
     * The default initial capacity - MUST be a power of two.
     */
    static final int DEFAULT_INITIAL_CAPACITY = 16;
    
        int capacity = 1;
        while (capacity < initialCapacity)
            capacity <<= 1;
    

    However, in Hashtable, I saw this:

    table = new Entry[initialCapacity];
    
    public Hashtable() {
        this(11, 0.75f);
    }
    

    So my question is: Why does HashMap require a power of 2 as the initial capacity, while Hashtable chooses 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.