2

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???
Keval Trivedi
  • 1,290
  • 2
  • 13
  • 29
  • 3
    Hint : `"Keval"!="Demo"` – TheLostMind Apr 16 '15 at 06:45
  • 2
    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. – Pshemo Apr 16 '15 at 06:46
  • 1
    s1 value has been changed before `hs.remove(s1);` statement – Rupesh Apr 16 '15 at 06:46
  • you are trying trying to remove an object that does't exist in hashSet to identify you must override .equals and .hashcode. – Key_coder Apr 16 '15 at 06:46
  • @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 Apr 16 '15 at 06:53

6 Answers6

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

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Can you elaborate your answer? because when i remove the s1 = new String("Demo"); from code it works fine. – Keval Trivedi Apr 16 '15 at 06:47
  • 2
    @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. – Pshemo Apr 16 '15 at 06:48
  • @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? – Keval Trivedi Apr 16 '15 at 06:56
  • 2
    @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. – Eran Apr 16 '15 at 07:05
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.

Community
  • 1
  • 1
Phoenix
  • 335
  • 2
  • 9
1

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.

Aakash
  • 2,029
  • 14
  • 22
0

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

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
0

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());
Prasanna Kumar H A
  • 3,341
  • 6
  • 24
  • 52
0

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.

Rajesh Kolhapure
  • 741
  • 1
  • 10
  • 21