Skip to main content

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


Comparison of HashMap and Hashtable source code in jdk 1.6, I saw below codes 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 below codes?




table = new Entry[initialCapacity];

public Hashtable() {
this(11, 0.75f);
}



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.



thx.


Source: Tips4allCCNA FINAL EXAM

Comments

  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.

    ReplyDelete
  2. 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".

    ReplyDelete
  3. 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

    ReplyDelete
  4. 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.)

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.