Possible Duplicate:
Why should hash functions use a prime number modulus?
Why is it necessary for a hash table’s (the data structure) size to be a prime?
From what I understand, it assures a more even distribution but is there any other reason?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The only reason is to avoid clustering of values into a small number of buckets (yes, distribution). A more even distributed hashtable will perform more consistently.
from http://srinvis.blogspot.com/2006/07/hash-table-lengths-and-prime-numbers.html
Update: (from original answer author)
This answer is correct for a common implementation of a hash table, including the Java implementation of the original
Hashtableas well as the current implementation of .NET’sDictionary.Both the answer and the supposition that capacity should be prime are not accurate for Java’s
HashMapthough. The implementation of aHashMapis very different and utilizes a table with a size of base 2 to store buckets and usesn-1 & hashto calculate which bucket to use as opposed to the more traditionalhash % nformula.Java’s
HashMapwill force the actual used capacity to be the next largest base 2 number above the requested capacity.Compare
Hashtable:https://github.com/openjdk/jdk/blob/jdk8-b120/jdk/src/share/classes/java/util/Hashtable.java#L364
To
HashMap:https://github.com/openjdk/jdk/blob/jdk8-b120/jdk/src/share/classes/java/util/HashMap.java#L569