File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding the role of hashcode() and equals()

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • 0
  • 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




 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • 0
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • 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 Java Oracle
  • 0
  • 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..!!
 
Martin Vajsar
Sheriff
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 0
  • 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
  • 0
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic