Why use a prime number in hashCode?

I was just wondering why is that primes are used in a class’s hashCode() method? For example, when using Eclipse to generate my hashCode() method there is always the prime number 31 used:

public int hashCode() {
     final int prime = 31;
     //...
}

References:

Here is a good primer on Hashcode and article on how hashing works that I found (C# but the concepts are transferrable):
Eric Lippert’s Guidelines and rules for GetHashCode()

9 Answers
9

Leave a Comment