File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

appropriate, legal and efficient hashCode()

 
Max Vandenburg
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Felipe Arcos Velez
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nilesh Patel
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Max Vandenburg
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
 
Max Vandenburg
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3665
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic