• 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

Regarding the role of hashcode() and equals()

 
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,
I was developing a program on hashset in which I have override the hashcode() and equals() but why we need to override ,If I make them as a comment in my pojo then the result of the hashmap did not come correct ..!!
can you guys after studying my short program , can explain the functionality of hashcode() and equals() with context to this program also, and general also..that will be a great help..!!

Emp.java



EmpHsDemo.java




 
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just comment out overriding equals and hashcode method and execute your program. You will come know.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please SearchFirst.(⇐ click) There's plenty of material available that covers these basic, high-level questions.
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohana Rao Sv wrote:Just comment out overriding equals and hashcode method and execute your program. You will come know.



I have done that and then after removing the elements it was still counting 6 elements instead of five ,please advise me te role of hashcode() and equals method () here..!!
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java hashcode tutorial

Saral, you really should try to find answers to your questions in publicly available resources first. Firstly, you'll learn what you need much sooner than when you to wait for someone here to answer your questions, and secondly, you'll learn how to find, use and utilize the existing documentation. Though it may seem trivial, some practice is needed to search for (and orientate oneself in) available resources. If you plan to earn a living as a software developer, this skill is indispensable.

It is possible that some things are still unclear even after reading the documentation and a tutorial or two. In this case, we'll be very glad to help. You'd ideally post links to materials you've read and pointed out the unclear parts. Asking smart questions is not only beneficial to you, but also to other users here at Javaranch.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saral Saxena wrote:please advise me te role of hashcode() and equals method () here..!!



Why do you refuse to search and do any research on your own, as I've suggested to you several times?
 
reply
    Bookmark Topic Watch Topic
  • New Topic