below is my java program. Please tell me why i am getting the size of hashset as 2 instead of 1 as i have overridden the equals and hashcode method.
import java.util.HashSet;
import java.util.Set;
public class EqualityCheck {
String first = new String();
String second = new String();
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EqualityCheck t1 = new EqualityCheck();
t1.first = "megha";
t1.second = "jain";
EqualityCheck t2 = new EqualityCheck();
t2.first = "j megha";
t2.second = "jain";
if(t1.equals(t2)){
System.out.println("t1 and t2 are equal");
System.out.println("t1 hashcode"+t1.hashCode());
System.out.println("t2 hashcode"+t2.hashCode());
}
Set<EqualityCheck> set1 = new HashSet<EqualityCheck>();
set1.add(t1);
set1.add(t2);
set1.add(t1);
System.out.println("set "+set1.toString()+"size = "+set1.size());
}
public boolean equals(EqualityCheck t){
System.out.println("inside equals method");
return true;
}
public int hashCode(){
System.out.println("inside hashcode method");
return 1111;
}
}
this is the output i am getting while running this in eclipse.
inside equals method
t1 and t2 are equal
inside hashcode method
t1 hashcode1111
inside hashcode method
t2 hashcode1111
inside hashcode method
inside hashcode method
inside hashcode method
inside hashcode method
inside hashcode method
set [EqualityCheck@457, EqualityCheck@457]size = 2