Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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

override hashCode()

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java:

By definition, if two objects are equal, their hash code must also be equal. If you override the equals() method, you change the way two objects are equated and Object's implementation of hashCode() is no longer valid.Therefore, if you override the equals() method, you must also override the hashCode() method as well.


I want to understand, how to override the hascode(). Say for example I have a class where I use:
String s3 = new String ("hello");
String s4 = new String ("hello");

if(s3.equals(s4) ){ // I am overriding the equals() method.
//do something....
}
HOW TO OVERRIDE HERE THE hashCode()?(need code example for this scenario.)

OR,
say another example of code I have is:
Test t = new Test();
Test p = new Test();
if(t.equals(p)){
//do something....
}
HOW TO OVERRIDE HERE THE hashCode()?(need code example for this scenario.)

I would appreiciate if someone can show the code example to override the hashcode() for above both examples.

thanks in advace friends...

[ June 09, 2008: Message edited by: Ramesh Kumar Swarnkar ]
[ June 09, 2008: Message edited by: Ramesh Kumar Swarnkar ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramesh Kumar Swarnkar:

if(s3.equals(s4) ){ // I am overriding the equals() method.
//do something....
}
HOW TO OVERRIDE HERE THE hashCode()?(need code example for this scenario.)



Ramesh,

You are not overriding the equals() method in your code... you are just using it. Overriding is done when you create a subclass and "redefines" an existing method in the parent class. For instance:



You may want to read more about method overriding and overloading.
 
Ramesh Kumar Swarnkar
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rodrigo,
I am agree with you, and tried to see the equals() and hashCode() simultaneous implementations in some codes in Internet; but could not find any easy example.
I could find a link: http://www.vipan.com/htdocs/hashcode_help.html
but, want to see a realy simpler example of code.If you know any such link can you just paste here.


thanks in advance,
Ramesh
 
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramesh

The String class is a final class so you cannot override equals() and hashcode().

With regard to finding a good hashcode algorithm to use in your own classes ... keep in mind that this is (at best) a nontrivial task. As the K&B book (which your original post quotes from) says ... people get their PHDs trying to write good hashcode algorithms.

All that aside, this Wikipedia link shows the String class hashcode algorithm and a simple one you could use for your classes.

One last thought ... the wrapper classes contain nice hashcode (and equals) implementations ... so I try to give objects I'll want store in a hashmap a Integer ID and use that.

hope this helps ..
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic