I read in an article which says:
It is a common practice to recommend a prime number to be the size of a hash table. This way, a funnel will occur only if the keys are multiples of the prime number.
Here, why should a prime number be used for the initial size, and what is funnelling?
Long answer: here
Short answer:
A funnel occurs when 2+ keys get hashed to the same value because of an error with the underlying hashing function. Like this:
If there is an inherent flaw in the hashing function used by
HashMap<K,V>, a “funnel” could occur where the keysabandbaboth map to, say,thing1.Think of a funnel as an actual funnel: multiple inputs get funneled into the same place.
Edit
If a hash function is flawed and does contain funnels, then a way to minimize their occurrence is to set the size of the table to being a prime number.
This is because, in terms of being a “class” (category) of numbers, prime numbers have the least amount of factors. According to that article, funnels occur when a given key is a factor or multiple of the size of the table. So if we set the table size to 100 (not a prime number), we introduce the possibility of funneling when the provided key is any factor of 100: 1, 2, 4, 5, 10, …, 100, 200, 300, etc.
But if we make the size of the table to a prime, say, 101… then the only possibility of a funnel occurs at: 1, 101, 202, 302, etc. We’ve greatly reduced the possibility of a funnel.