• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

HashSet and Equals and HashCode

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

I have not overridden the equals and hashcode method in the below mentioned code. I am unable to get, why contains method returns true.



 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neeraj Vij wrote:Hi All,

I have not overridden the equals and hashcode method in the below mentioned code. I am unable to get, why contains method returns true.



Which "true" are you not able to understand? If its this: "checkEquals2 == checkEquals1 -> true" - Then in your code you explicitly assigned them to be equal: checkEquals2=checkEquals1;
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
contains() uses equals(). If you don't override the equals() method, an instance of a class will only be equal to itself. If you add A into the set, and check if the set contains A, it will be true, because A.equals(A) is true.

What do you mean you can't get ?
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thanks both for your inputs.

I was expecting the following to return false


then why its required to overide equals when using custom objects with hashset?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
checkEquals.equals(checkEquals1) returns false because checkEquals and checkEquals1 are different instances. testSet.contains returns true for the reason I have explained above.

then why its required to overide equals when using custom objects with hashset?


When you don't want two objects meaningfully equal to be in the same set. Let's say that you don't want two CheckEquals instance with the same name to be there. With your actual code, you can insert two different instances with the same name :


Both will print true, proving that both instances were inserted in the set.
If you want to avoid that, you'll have to override equals() and hashCode():


(not testing for null here)
Now, true and false will be output. The second instance will not be inserted because the equally same object was already in there.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton for your clarification...I got it...it will work when you have the exact reference
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same. containsKey will use the equals() method, which explains why youre getting true. If you don't override hashCode, all your objects will end up in the same bucket, which is not very efficient if you have many objects in your map. That won't prevent you from fetching them though.
 
Don't play dumb with me! But you can try this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic