• 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

Explain

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

Anybody explain equal() and hashmap() method in collection
Why we overload etc ?
With one small example
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals and hashcode method are present in the Object class and are inherited into the class that we define. Well in the object class equals() method gives out the same answer as == i.e if the references point to the same object then they are equal. hashcode() will give different hashcodes for different objects everytime putting them in different buckets.

Actually equals method should compare the values in the objects and then give the result. As for Wrapper and String classes the equals method is implemented and thus will give the comparison between the object values.


well same goes for hashcode(). If you implement the hashcode() according to you need so that the objects go in a specific bucket rather than each object getting its own bucket. there is no harm in getting its own bucket but it will allow duplicates to get different buckets thus using up the space. If hashcode() method is implemented then you can minimize the adding of duplicates in different buckets.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Discussed plenty of times. Search the same forum
And also it is hashCode() not hashmap(), the methods must be overridden for client class if you're storing the client class object into HashMap.

 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vineet walia wrote:
Anybody explain equal() and hashmap() method in collection
Why we overload etc ?
With one small example



You don't always override hashCode() method when you override equal() method. It depends on the requirement. Collection classes such as HashMap and HashSet depend on the fact that the objects that you put into them follow the hash code contract. If the objects do not follow the contract, then when you try to store them in a HashMap or HashSet, strange behaviour can happen.

You can refer : http://www.artima.com/lejava/articles/equality.html
https://coderanch.com/t/265099/Programmer-Certification-SCJP/certification/hashCode-contract#2004103

Note: its hashCode() not hashmap(), and we do not overload, we override them.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sagar Rohankar wrote:And also it is hashcode() not hashmap(), the methods must be overridden for client class if you're storing the client class object into HashMap


It's hashCode(), not hashcode(). You will expected to know the difference and the implications for the exam.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jason, I corrected my mistake.
 
vineet walia
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please provide my link of that form
 
vineet walia
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish Bangera wrote:equals and hashcode method are present in the Object class and are inherited into the class that we define. Well in the object class equals() method gives out the same answer as == i.e if the references point to the same object then they are equal. hashcode() will give different hashcodes for different objects everytime putting them in different buckets.

Actually equals method should compare the values in the objects and then give the result. As for Wrapper and String classes the equals method is implemented and thus will give the comparison between the object values.


well same goes for hashcode(). If you implement the hashcode() according to you need so that the objects go in a specific bucket rather than each object getting its own bucket. there is no harm in getting its own bucket but it will allow duplicates to get different buckets thus using up the space. If hashcode() method is implemented then you can minimize the adding of duplicates in different buckets.




thank you so much i got it
if you can provide any more information then please give me
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i think K&B provides enough information. Also sun tutorials are also good to get the information. But i prefer you keep it simple.
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic