• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

HashCode and Equals implementation

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 43
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic