Reputation: 1300
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???
Upvotes: 2
Views: 25840
Reputation: 393811
String s1 = new String("Keval");
....
hs.add(s1);
....
s1 = new String("Demo");
hs.remove(s1);
You are adding a String
which is equal to the String
"Keval" to your HashSet
, but you are trying to remove a String
equal to the String
"Demo" from the Set
.
Your HashSet
contains no String
equal to "Demo", so the remove()
call removes nothing from the Set
, and doesn't affect its size.
When you remove the s1 = new String("Demo")
line, s1
still refers to the String
that was added to the Set
(the one which is equal to "Keval"), so hs.remove(s1)
removes that String
from the Set
.
Upvotes: 10
Reputation: 3431
You are initializing the string as new String which will create new one in the string pool.If you remove before changing the string value it will remove from hash set and will give size as two(Its because when you added hs.remove
,s1 value is "Demo" not "keval".
String s1 = new String("Keval");
hs.add(t1);
hs.add(t2);
hs.add(s1);
System.out.println("Set Size :: " + hs.size()); //3
s1 = new String("Demo");
hs.remove(s1); //equals() method fails to find Demo and none of the element will be removed
System.out.println("Set Size :: " + hs.size());
Upvotes: 0
Reputation: 771
HashSet finds bucket/location in hashset by calculating hashcode of key. While adding s1 in hs your key was "Keval" which will generate a hashcode and s1 object will be stored in that bucket. Now you have changed s1 to "Demo". While removing s1, bucket will be searched based on hashcode generated from key "Demo". This bucket is not found in hs and hence its not deleted. Hashcode of "Keval" and "Demo" is not same. Hope this clears your confusion and answers your query.
Upvotes: 0
Reputation: 333
If you want the Hashset to identify your object, you will have to override the equals and hashcode method.
Since you added "Keval" and tried to remove "Demo" there are no changes to set.
Remember since you are using HashSet of Objects, be careful while playing with hashcode and equals method that may have unintended consequences. See this question for more detail about this.
Upvotes: 2
Reputation: 2109
Your problem have multiple issues, and I think you should learn a few basics.
Whenever you do a new, it creates a new object.
s1 = new String("Demo");
Hashset works on object's hashcode()
and equals()
. So if you are using your own class to be added to Hashset, please override both of these methods. For more learning, please google them.
Now for your problem, when you created a new object by doing
s1 = new String("Demo");
and then trying to remove that new object from hashset by hs.remove(s1);
, hashset will use methods equals()
and hashcode()
to identify the object that should be removed. Since this new object is not present in the hashset, nothing will be removed.
Hence the size is un-changed.
Upvotes: 1
Reputation: 13556
After you do s1 = new String("Demo")
, reference s1
is simply referring to the new object and not the one on HashSet.
So it is not removing anything from set
Upvotes: 0