please any one explain this hashcode ovveriding and equals method internally how it will excute. and also which method executes first, here i used only 2 elements if i want for more than 3 elements how to write.
hashcode ovveridden means unique hashcodes are providing based on body implemention and improves retriving the elements.
here i written in hashcode body return empid means based on empid it will return hashcode for that object or some other else.
if writing instead of empid, 55 what is the meaning of this.and getting output same. and also what is the workflow behind these.
please let me know as soon as possible
Joined: Jul 05, 2011
when you call set.add() method it internally calls first equals method and compare present object hashcode with previous objects hashcode. by default for every object hashcode is different.so it will accept duplicates.
now if you dont want to allow duplicates in your hashset you have to over ride equals and hashcode. what it means duplicate object?
if object contains same values then it is duplicate.
in your example you override hashcode with id. but if id and name is equals then it is duplicate. so you have to override hashcode with every value of that object.
for your example
if you want to add more objects in set better to create separate bean class with setter and getter methods. and create object for that class and add values and add each object to set. and over ride hashcode and equals method