You are on page 1of 3

6/13/13

appropriate, legal and efficient hashCode() (SCJP forum at JavaRanch)

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author
Max Vandenburg Ranch Hand Joined: Mar 09, 2006 Posts: 51

appropriate, legal and efficient hashCode()


posted 8/28/2006 7:18 PM

hashCode#1
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 .

p u b l i ci n th a s h C o d e ( ){ r e t u r n1 ; }

hashCode#2
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 .

p u b l i ci n th a s h C o d e ( ){ r e t u r nt h i s . s o m e V a r * 5 ;/ / a s s u m i n gs o m e V a ri sa l r e a d yi n i t i a l i s e d }

hashCode#3
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 .

p u b l i ci n th a s h C o d e ( ){ / /s o m ec o d et h a td i s t r i b u t ek e yr a n d o m l y / /s ot h a tw h i c h e v e r" h a s h C o l l e c t i o n "i su s e ds h a r eam i n i m a l e no b j e c t s }

h a s h C o d eb e t w e

which of the above has code is legal, appropriate and efficient? [ August 28, 2006: Message edited by: Firman Drage ]
MV Felipe Arcos Velez Greenhorn Joined: Mar 30, 2004 Posts: 9

posted 8/29/2006 1:20 AM

First of all, check java impl recomendations for object.hashcode: extract from Java Docs: * Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode

www.coderanch.com/t/258723/java-programmer-SCJP/certification/legal-efficient-hashCode

1/3

6/13/13

Posts: 9

appropriate, legal and efficient hashCode() (SCJP forum at JavaRanch)


* Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. * If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. * It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. So, the first implementation doesn't return distinct values for each object, the second calcs a expression but can you be sure that somevar variable is unique for each object?, and the third implementation can be apropiate if the assigned random key is constant during object life.

Felipe Arcos Velez

Nilesh Patel Ranch Hand Joined: Feb 02, 2006 Posts: 91

posted 8/29/2006 11:51 AM

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashC ode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.

If we use Third Hash method than may be possible that i will give diffrent int for same object because we have choose random value from file....

Nilesh Patel SC JP 1.5 - 87% Max Vandenburg Ranch Hand Joined: Mar 09, 2006 Posts: 51

posted 8/29/2006 1:25 PM

so we all agree that they all are legal hashCode... given that some are more efficient that others. but its it safe to say that all the hashCode above are an "appropriate" implementation of hashCode?

wise owen Ranch Hand Joined: Feb 02, 2006 Posts: 2023 Max Vandenburg Ranch Hand Joined: Mar 09, 2006 Posts: 51

posted 8/29/2006 6:54 PM

Effective Java Programming Language Guide

posted 8/31/2006 1:13 PM

Thanks

Paul Anilprem Enthuware Software Support Ranch Hand Joined: Sep 23, 2000 Posts: 2920

posted 8/31/2006 3:33 PM

Originally posted by Firman Drage: so we all agree that they all are legal hashC ode... given that some are more efficient that others. but its it safe to say that all the hashC ode above are an "appropriate" implementation of hashC ode?

Legality of a hashCode() method is always determined with respect to the equals() method of the same class. For example, the hashCode() method in #2 will not be legal if the equals method is as follows: class X{ ... public boolean equals(Object x){ return this.someOtherVar == ((X)o).someOtherVar; ... }

www.coderanch.com/t/258723/java-programmer-SCJP/certification/legal-efficient-hashCode

2/3

6/13/13

appropriate, legal and efficient hashCode() (SCJP forum at JavaRanch)

Efficency of a hashcoding mechanism ( note that this is different from efficiency of the method itself) is pretty much a mathematical calculation. It is possible to determine which hashcode mechanism distributes more evenly. For example, i%5 is more efficient than i%2 (T&C apply ).

Appropriateness of the hashCode() method can be quite subjective. Only the usage of the hashcode can determine if it is appropriate or not. For very simple cases where an application is making use of the hashcode very rarely, mechanism in #1 may be more appropriate than using a complex algorithm that takes 5 seconds to compute a very efficient hashcode. But then if you are storing and searching a million instances investing in a time consuming hashCode method might be worth it.

HTH, Paul.

Enthuware - Best Mock Exams and Questions for Oracle/Sun Java C ertifications Quality Guaranteed - Pass or Full Refund!

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: appropriate, legal and efficient hashCode()

Similar Threads hash code and final variables in hashCode( ) and equals( ) hashtable working & overriding equals Random question
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 13, 2013 03:06:32 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/258723/java-programmer-SCJP/certification/legal-efficient-hashCode

3/3

You might also like