aspose file tools*
The moose likes Beginning Java and the fly likes hashcode and equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hashcode and equals" Watch "hashcode and equals" New topic
Author

hashcode and equals

Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324


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 ]

SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
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
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
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

Joined: Sep 28, 2008
Posts: 324
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
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
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!
Lutz Hankewitz
Greenhorn

Joined: Dec 10, 2007
Posts: 5
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
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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,


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
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

Joined: Sep 28, 2008
Posts: 324
I would defintely like to have my class see the dayligt.
"Override Hashcode" -- Himalay

Thank you all for the valuble suggestions.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
And survive the users' reaction to your class!
 
Don't get me started about those stupid light bulbs.
 
subject: hashcode and equals
 
Similar Threads
StackOverflowError using hashCode() in HashMap in recursive method
S&B hashCode() clarification needed.
Hashcode and equals contract - K&B Mock
Overriding equals() and hashCode() methods
Properly overriding equals and hashCode