Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Regarding the role of hashcode() and equals()

 
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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?
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic