File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes writing efficient hashcode implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "writing efficient hashcode implementation" Watch "writing efficient hashcode implementation" New topic

writing efficient hashcode implementation

anish jain
Ranch Hand

Joined: Feb 03, 2010
Posts: 129
How to write an efficient hashcode implementation so that tha hashcodes are uniformly distributed?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37926
There is a nice example in Effective Java by Joshua Bloch. The same technique is copied into Bruce Eckel's Thinking in Java, which you may be able to find a copy of the 3rd edition on the net (legally).
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225


Please check apache commons lang

org.apache.commons.lang.builder.HashCodeBuilder ( to build HashCode ...)

org.apache.commons.lang.builder.EqualsBuilder ( to generate Equsl() based on fields of class )

Thanks, Ganesh Gowtham
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37926
Thank you. Those Apache Commons methods might give effective hash and equals methods, but using such tools does not help one learn the algorithm, I am afraid.
I agree. Here's the link:
subject: writing efficient hashcode implementation
Similar Threads
Clarification needed on K&B use of "legal" and "appropriate" regarding hashCode() implementations
Entity bean with composite key
identical hashcode values
appropriate, legal and efficient hashCode()
variables in hashCode( ) and equals( )