my dog learned polymorphism*
The moose likes Java in General and the fly likes Regarding the role of hashcode() and equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding the role of hashcode() and equals()" Watch "Regarding the role of hashcode() and equals()" New topic
Author

Regarding the role of hashcode() and equals()

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

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

Joined: Aug 01, 2007
Posts: 485

Just comment out overriding equals and hashcode method and execute your program. You will come know.


ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Please SearchFirst.(⇐ click) There's plenty of material available that covers these basic, high-level questions.
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

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

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding the role of hashcode() and equals()
 
Similar Threads
HashSet
Duplicates in HashSet
Sorting a Vector by a specific language?