Professional Documents
Culture Documents
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
Author
Max Vandenburg Ranch Hand Joined: Mar 09, 2006 Posts: 51
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 .
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
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
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
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
Thanks
Paul Anilprem Enthuware Software Support Ranch Hand Joined: Sep 23, 2000 Posts: 2920
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
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
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 .
www.coderanch.com/t/258723/java-programmer-SCJP/certification/legal-efficient-hashCode
3/3