This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes hashCode method implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hashCode method implementation" Watch "hashCode method implementation" New topic
Author

hashCode method implementation

wrushasen dakhane
Ranch Hand

Joined: Sep 25, 2006
Posts: 47
Consider we have

Employee e1 = new Employee("TCS");
Employee e2 = new Employee("GS");
Employee e3 = new Employee("TCS");

where Employee is our class.

How should we implement the hashCode method so that we can have same hasCode value for objects e1 and e3.
Best Regards,
Wrushasen D.


SCJP 1.4, SCWCD 1.4
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

In your hashcode method, return the hashcode() for the field that stores the employee name or whatever "TCS"/"GS" stands for. eg:

Also, in your equals method you should only test for the equality of the employee name in this case. So, that your equals and hashCode methods are consistent. eg:


apigee, a better way to API!
 
Consider Paul's rocket mass heater.
 
subject: hashCode method implementation
 
Similar Threads
hashCode method implementation
garbage collection
How to implement hashCode()
Custom keys in tree map
hashCode() - why?