aspose file tools
The moose likes Java in General and the fly likes Why Prime number for Hashcode?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why Prime number for Hashcode??" Watch "Why Prime number for Hashcode??" New topic
Author

Why Prime number for Hashcode??

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 362

Hi All,

I would like to know that why do we use prime number in Hash code implementation.


Pawan Chopra
SCJP - DuMmIeS mInD
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
I don't have deep knowledge on the implementation of hashing algorithms, but I know that using prime numbers minimizes collisions. See Wiki for a short explanation.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
It is more likely to produce slight differences in the right-hand bits of the number. Since HashMap etc use something analogous to hash % 16, hash % 32 etc, the lower-order bits are where differences are beneficial.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12928
    
    3

We recently had a topic with a similar question and I wrote a long answer to it. Read it here.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 362

Jesper Young wrote:We recently had a topic with a similar question and I wrote a long answer to it. Read it here.


Thanks Jesper I should have done some search on this before posting this question thanks alot great explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Prime number for Hashcode??
 
Similar Threads
overriding equals method in my class
hashing and prime numbers ?
problem displaying prime no program.
WA #1.....word association
Why is 31 taken in finding the hashCode of String?