• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashCode method implementation

 
wrushasen dakhane
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic