I have started learning Collections
. So when we generate hashcode
using eclipse below is the formula which is present in the method:
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((pin == null) ? 0 : pin.hashCode());
I have searched and found that since 31
is odd prime
we use it while calculating hashcode
. Multiplying by prime gives a good distribution of hashcodes.But haven't come across any concrete/layman explaination on why do we use the above formula and why exactly 31 is used. Can someone please help elaborate on how exactly does multiplying by 31 give a better distribution of hashcode?