• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashcode and equals

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


In the above code if I override just equals method and NOT hashcode it allows me to insert duplicate objects into the Set, as expected.

In case NO Set or Map is being used by me. I just try to compare two objects (Value Object, Composite Object or Domain Object). Is there ANY specific reason() for me to maintain the equals and hashcode contract in this case.

The API doc for equal says: Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes

[ November 13, 2008: Message edited by: Himalay Majumdar ]
[ November 13, 2008: Message edited by: Himalay Majumdar ]
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not overriding hashCode is a bit like having three cars, all with the same registration number! You can get done for dangerous driving in several places simultaneously.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get away with nor overriding hashCode if you can arrange a PutIntoHashCollectionException which is thrown whenever your object is put into a hashed Collection or Map.
Or something you can do at home for your own interest as long as you can be sure nobody else is trying to put your objects into their HashMaps.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question came out in my mind thru a discussion with my friend. As per him overriding hashcode is a good practice but its not needed until we use HashMap or HashSet.

I wonder why dint I join javaranch before, this is a pretty active forum.

Thanks Campbell
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is only actually used when you try using a Hashed Collection, but once you let a class loose in the big wide world it is only a matter of time before somebody tries to put it into a HashMap or HashSet. Then they will curse if you haven't overridden hashCode properly.

The alternative is this, which should give a different result every time it is used.That should improve your popularity no end!
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A littel bit dirty but still avoiding havoc is to implement
the hashCode method to return just one constant value
e.g.
public int hashCode(){
return 42;
}

this forces the Set/Map to call to equals method when inserting an object in the 42 bucket, but
results in a very bad performance because all elements of the same class are but in a single bucket list with O(n) search time for lookup.

But I would prefer the Exception solution over this one to have the safe feeling to be at leaste notified that it is time to implement it in a professional manner.

Kind regards

Lutz
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the class will never see the light of day anywhere else, then NO you don't need to implement hashCode.

you can certainly easily implement it to return data.hashCode and be done with it as well,
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or data.hashCode() + num. Assuming data isn't null. There ought to be a constructor in that class, which ensures that the data field isn't null.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would defintely like to have my class see the dayligt.
"Override Hashcode" -- Himalay

Thank you all for the valuble suggestions.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And survive the users' reaction to your class!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic