Remove element from HashSet

24,300

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.

  1. Whenever you do a new, it creates a new object. s1 = new String("Demo");

  2. 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.

  3. 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.

Share:
24,300
Keval Trivedi
Author by

Keval Trivedi

Updated on February 18, 2020

Comments

  • Keval Trivedi
    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
      TheLostMind about 9 years
      Hint : "Keval"!="Demo"
    • Pshemo
      Pshemo about 9 years
      Not that it answer your question but you should implement hashcode and equals methods in your Test class if you want to use them in hash structure like HashSet.
    • Rupesh
      Rupesh about 9 years
      s1 value has been changed before hs.remove(s1); statement
    • Key_coder
      Key_coder about 9 years
      you are trying trying to remove an object that does't exist in hashSet to identify you must override .equals and .hashcode.
    • Keval Trivedi
      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
    Keval Trivedi about 9 years
    Can you elaborate your answer? because when i remove the s1 = new String("Demo"); from code it works fine.
  • Pshemo
    Pshemo about 9 years
    @KevalTrivedi Your added to your set new String("Keval"); but are trying to remove new String("Demo"); which are not equal objects. In other words you are trying to remove object which doesn't exist in set.
  • Keval Trivedi
    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
    Eran about 9 years
    @KevalTrivedi It probably doesn't even get to run the equals method, since the hashCode is most likely different, so the two Strings are probably mapped to different buckets.