import java.util.*;
class AB
{
int i;
public boolean equals(Object o){
if(!(o instanceof AB)) return false;
return ((AB)o).i==i;
}
@Override
public int hashCode() {
return i;
}
AB(int i){this.i=i;}
}
public class demo5
{
public static void main(
String []args){
HashSet<AB> s=new HashSet<AB>();
AB obj1=new AB(6);
s.add(obj1);
System.out.println("Size:"+s.size());
AB obj=new AB(6);
s.add(obj);
System.out.println("Size:"+s.size());
}
}
Now your program will work as you want.