Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

overiding hashCode method from Object Class

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand when overiding the equals method the hashCode method should also be overidden. Can i get any help on how to acheive this?
Thanks
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, you should take a look at the hashCode agreement when you are overriding hashCode(). Here it is, you can find this in the Java API in the Object class.


public int hashCode()Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
The general contract of hashCode is:
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.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)


So bascially you want to return an integer for Objects, but if two objects have the same value you need to guarentee that their hashCode values are the same.
Lets say that I had a class:
 
Amit Chohan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ibrahim
one more question(should these questions be in Beginner forum?)
The following constructor creats a turtle with specified values.
public Turtle(int x, int y, char dir){
this.x = x;
this.y = y;
this.dir = dir;
}
i want to overide the compareTo method so that it returns an integer value (e.g 0 if equal).
Turtle bob = new Turtle(30, 40, 'N');
Turtle john = new Turtle(40, 50, 'S');
how i compare these two turtle objects. I wrote this but its totally wrong -
public int compareTo(Object obj){
Turtle tr = (Turtle)obj;
return (this.dir== tr.dir) && (this.x == tr.x) && (this.y == tr.y);

}
thanks
 
Amit Chohan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please ignore the last query, if i do not understand it then you will not either!!
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First you need to decide what it means for one Turtle object to be "larger than" or "smaller than" another.
And your definition needs to be consistent.
Among other rules:
You can't have a<b and also b<a.
If a<b and b<c, you'd better have a < c.
 
Ranch Hand
Posts: 427
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read Josh Bloch's book "Effective Java"
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is some guidance for implementing these
methods, along with some examples:

hashCode:
http://www.javapractices.com/Topic28.cjp
equals:
http://www.javapractices.com/Topic17.cjp
compareTo:
http://www.javapractices.com/Topic10.cjp
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amit Chohan:
one more question(should these questions be in Beginner forum?)


WHAT!!! And undermine all my subversive effort to overtake the Beginners Forum . Watch your language Sir!!
 
Amit Chohan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still trying to write a compare to method but am struggling.
I want my compare method to return an int value. 0 for equal, negative for less and positive for greater than.
Attributes are
String name;
int time;
raining in england
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find the compareTo(Object) method of String helpful.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once again, you need to think this through carefully. What does it mean for one Appointment to be "less than" or "greater than" another? Does your definition produce a consistent, total ordering?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic