• 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

equals method override

 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can anybody explain If we don't override the equals method we are not able to add that object as a key in the hashtable and we won't get the accurate set also why ?

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

S Majumder wrote:Can anybody explain If we don't override the equals method we are not able to add that object as a key in the hashtable and we won't get the accurate set also why ?


First try out with a program without overriding equals method and add some objects in hashtable...
Then same thing with overriding equals method...
You will get your answer
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Satya,

When we work with the collection we talk about the objects being meaning fully equal, if you are using a custom object as a key so while retrieving we check the equality based on some attribute of the object. To check the equality we override the equals method and write the logic there.

Thanks
Suresh
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suresh/Gaurangkumar ,
Basically for searching of an object into the collection the equals method comes into the picture , correct me if am wrong ..
Why we not able to add that object into the hashtable ?
Why the set isn't give correct output ?

-- Satya
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lets say you have a HashSet which is an unordered set. now you know that Set cannot contain duplicates. when we say 'No duplicates' that mean 2 or more objects should NOT be equal. here you can see the need of establishing equality of 2 or more objects. In case of Maps , suppose you have a class Car . now consider you have a Map which maps 'Car' objects to String representing owner of the car. You constructed a Car object with properties say color=red, gear=automatic and added it to the Map. now suppose you don't have the reference of that Car object you added and you want to retrieve the owner corresponding to the car you just added. what will you do ? may be you thing that let me make a new Car object Car c = new Car("red, "automatic") and use that object to retrieve the owner. but since you haven't overridden the equals method in your Car class, the new Car object (Car c = new Car("red","automatic")) will be different from the one you added. so you won't be able to find the owner. so you have to establish the equality of the Car objects in some way. how the equality is to be defined is depending upon your need .
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Satya,

I would highly recommend to go through hashCode and equals method related section from Bloch's book (Effective Java).
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi gurpeet ,
Thanks a lot for the explanations .
Anayonkar ,
Will get the book very soon ..

-- Satya

 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic