Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

overriding hashcode and equals method internal work flow

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
any body explain to me overriding hashcode and equals methods
in collections especially.

import java.util.HashSet;
import java.util.Set;

public class Employee {

int empid;
String empname;
static Employee e1, e2;

Employee(int id, String name) {
empid = id;
empname = name;
}

public static void main(String[] args) {
e1 = new Employee(12, "lod");
e2 = new Employee(12, "lod");


boolean a;
Set hs = new HashSet();
a = hs.add(e1);
System.out.println(a);
a = hs.add(e2);


System.out.println(a);
System.out.println(hs);

}
@Override
public int hashCode() {
//System.out.println("Hi");

return empid;
}


public boolean equals(Object o) {

//System.out.println("hi1");
if (e1.empid == e2.empid) {
return true;
} else {
return false;
}
}


}

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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Java.util.HashSet

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


and java bean class


in this case i validated if object contains same values for id and name and designation that is duplicate.if you want you can validate using only name or id or designation

please visit my java site for more info
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic