This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes hashCode implementations 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 "hashCode implementations" Watch "hashCode implementations" New topic
Author

hashCode implementations

Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Often I see in hashcode implementation examples multiplying some member variable by 37 (or some prime number).

For example,


What is the purpose of this? My guess would be this is to help the case when you have two objects such that
Without that multiplier, both would have the same hashcode (which is okay, but maybe less efficient). Is there another purpose to this?

Thanks,
Jeff
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
From what I understand the prime numbers are used for avoiding collisions.

See the following link for a detailed explanation

Hashtable


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

True, although we say "reduce collisions" rather than "avoid collisions". Collisions happen, just hopefully not too often.

Nice link though, worth reading.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32651
    
    4
Originally posted by Jeff Storey:


You did mean . . .. . . didn't you?
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: hashCode implementations
 
Similar Threads
Doubt regarding the hashCode value
equals() and hashCode() from Java.Inquisition
SJCP - equals() question
hashCode implementations
Inheritance and incomptaible casting