First of all add the element in HashSet and prints the size of HashSet which returns as expected. but i modified one of the object value and again store in to HashSet and remove the object using object name. but still i get the same size as previous. My code is as under :
public class Test {
private String s;
public Test(String s){
this.s = s ;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<Object> hs = new HashSet<Object>();
Test t1 = new Test("Keval");
Test t2 = new Test("Keval");
String s1 = new String("Keval");
hs.add(t1);
hs.add(t2);
hs.add(s1);
System.out.println("Set Size :: " + hs.size());
s1 = new String("Demo");
hs.remove(s1);
System.out.println("Set Size :: " + hs.size());
}
}
Output of above code is :
Set Size :: 3
Set Size :: 3 // Why it prints 3 insted of 2???