Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes appropriate, legal and efficient hashCode() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "appropriate, legal and efficient hashCode()" Watch "appropriate, legal and efficient hashCode()" New topic
Author

appropriate, legal and efficient hashCode()

Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
hashCode#1

hashCode#2

hashCode#3


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 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
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.


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
SCJP 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
Effective Java Programming Language Guide
Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
Thanks
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3314
    
    8
Originally posted by Firman Drage:
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?


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;
...
}


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 Certifications
Quality Guaranteed - Pass or Full Refund!
 
wood burning stoves
 
subject: appropriate, legal and efficient hashCode()