Remove element from HashSet
Solution 1
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
.
Solution 2
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.
Solution 3
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()
andequals()
. 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 byhs.remove(s1);
, hashset will use methodsequals()
andhashcode()
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.
![Keval Trivedi](https://i.stack.imgur.com/mn6UP.jpg?s=256&g=1)
Keval Trivedi
Updated on February 18, 2020Comments
-
Keval Trivedi over 4 years
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???
-
TheLostMind about 9 yearsHint :
"Keval"!="Demo"
-
Pshemo about 9 yearsNot that it answer your question but you should implement
hashcode
andequals
methods in yourTest
class if you want to use them in hash structure like HashSet. -
Rupesh about 9 yearss1 value has been changed before
hs.remove(s1);
statement -
Key_coder about 9 yearsyou are trying trying to remove an object that does't exist in hashSet to identify you must override .equals and .hashcode.
-
Keval Trivedi about 9 years@Rupesh You are trying to say that if i modified the vlaue of hashset object and after that i remove the object from HashSet it will not remove the object and it is still point to that object?
-
-
Keval Trivedi about 9 yearsCan you elaborate your answer? because when i remove the s1 = new String("Demo"); from code it works fine.
-
Pshemo about 9 years@KevalTrivedi Your added to your set
new String("Keval");
but are trying to removenew String("Demo");
which are not equal objects. In other words you are trying to remove object which doesn't exist in set. -
Keval Trivedi about 9 years@Eran Yes i got your point. its check the value using equals method and it's returns false. that's why it is not removing the element. am i right?
-
Eran about 9 years@KevalTrivedi It probably doesn't even get to run the
equals
method, since thehashCode
is most likely different, so the two Strings are probably mapped to different buckets.