• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashCode and Equals implementation  RSS feed

 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

Its that very old question regarding hashcode and equals implementation which i am not getting .



In the above program even if i comment out the Hashcode method , i believe it is still taking the memory address values from the native hashcode method of Object class. but the equals override implentation says that i have two insertions which are same . So as per my logic it should not allow the duplicate element to enter.but its not so ...the duplicate element is well inserted without hashcode .Please help me to understand how...
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand your question.
Your implementation of hashCode/equals is correct (regarding equals/hashCode contract).
What is not clear?

Please, when posting UseCodeTags ← click this

And welcome to the Ranch!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Part of the contract of equals/hashcode is that if two objects are equal (according go the equals() method) then they must have the same hashcode.

If you use the default hashcode method from Object then all objects will have a different hashcode*. Any class using equals/hashcode may get confused if your equals method returns true but the objects have different hashcodes.

*Actually the hascode contract in Object only says that this will be true as far as is reasonably practical.
 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi mike ,

i got you.but please help me out of the statement item.hashCode(). If i am not wrong then this is the hashCode method of Object class that is been called over here.
now the problem is the item value that is" banana" is stored by two different elements . one by add() method and other by duplicate.
so the return value should be different for different memory addresses. but they are not actually so.


 
Amar Saikia
Ranch Hand
Posts: 43
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i am not wrong then this is the hashCode method of Object class that is been called over here


You are correct. The hashCode method of Object class is been called over here. However, here the object is a String and and you may be aware that Equals and HashCode method of String is already overridden. So, for same string (here 'Banana'), even though you are calling multiple times, you will still get the same value.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
luke brown wrote:i got you.but please help me out of the statement item.hashCode(). If i am not wrong then this is the hashCode method of Object class that is been called over here.

Nope. You are wrong. Here the method hashCode() of String class is called. Hash codes for two equal strings will always be the same.


luke brown wrote:(...) so the return value should be different for different memory addresses. but they are not actually so.

Note that nothing in Object#hashCode says that hash code will be computed based on memory address (but in most cases it is).
javadoc wrote: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.)

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
luke brown wrote:i got you.but please help me out of the statement item.hashCode(). If i am not wrong then this is the hashCode method of Object class that is been called over here.

I suspect that you're obsessing too much about the mechanics here.

Back up a bit: What is a "Price", and how do you want it to behave? Specifically: What do you want a HashSet<Price> to do? Then write equals() and hashCode() methods that fulfil those goals.

Personally, I'm not sure that you actually need a Price class at all (at least not at this stage), since the way you've written it, it would appear to simply be an attribute of an Item (Note: an Item, NOT a String). [*]

It's possibly also worth mentioning that there is a big difference between a "price" (the RRP of an Item), which you probably don't want duplicated, and a "line-item price" (the price that appears in a sales receipt), which you almost certainly do.

HIH

Winston

[*] Indeed, I suspect you could achieve this by simply renaming your current class to Item, and renaming its "item" field (and getter method) to "name".
 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
luke brown wrote:i got you.but please help me out of the statement item.hashCode(). If i am not wrong then this is the hashCode method of Object class that is been called over here.

Nope. You are wrong. Here the method hashCode() of String class is called. Hash codes for two equal strings will always be the same.


luke brown wrote:(...) so the return value should be different for different memory addresses. but they are not actually so.

Note that nothing in Object#hashCode says that hash code will be computed based on memory address (but in most cases it is).
javadoc wrote: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.)




Thanks pawel,

Got it now its the String.hashCode () that is taking the value "banana" (and not any address) doing some gimmicks over it and returns it back .So the two values ought to be same.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!