The set contains the values 45 and 46. Then you print the size. When you remove i1, you tell it to remove the value 45. Then, when remove i2, you tell it to remove the value 46.
Now, what happens when you add the line i2 = 47? The set contains the value 46, and you tell it to remove the value 47, which isn't contained by the set. So the resulting set should still contain the value 46.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Joined: Jan 14, 2011
Thanks for your reply Stephan! But I still didn't get it..
The set still contains the value 46 instead of 47 after adding line 14, that means 47 wasn't contained by the set, so how come it could be removed if it wasn't contained?
Hello there, i slightly modified your code . if you observe i added an extra println statement after the statement "i2=47;"
This new print statement will print out 47, and the elements in the set will be 45 & 46.
so, when we say s.remove(i2), are we removing the i2's value(47) which is just assigned a value but not added into the collection ?
Joined: Jan 14, 2011
I did have the same question as yours when I read this code, and this should refer to the concept of "Stack and Heap storage" (even I am not sure...)
This is my thought so far.. Hope that anyone can correct my assumption if there is any mistake??
To my mind, i understand the code like this (I might be wrong too) - I apologize by advance if i'm wrong :
// 1) Here we declare i2, a reference to an Integer Object,
// 2) We create an object (Integer) with the value 46 (It's the boxing way, it means =46 it's the same as new Integer(46))
// 3) We affect the reference of our object (Integer with value 46) to i2
// 4) We add the Reference (not the value) in the HashSet
// 5) We create an object (Integer) with the value 47 (It's the boxing way, it means =47 it's the same as new Integer(47))
// 6) We affect the reference of our object (Integer with value 47) to i2
// Note : Here we still have a reference to the Integer 46 in the set so no Garbage Collector, isn't it ?
// 7) We try to remove in s the same reference as i2 contained
// But in s we don't have in the set a reference to the integer object 47 so nothing it's removed
// 8) Here we create an integer object with the 46 value
// 9) here it's possible to remove the 46 value because Integer as the equals method so the contains method return true
// 10) It's the same "effect" as the point 8 and 9
// We create an object (Integer) with the value 46 (It's the boxing way, it means =46 it's the same as new Integer(46))
// We affect the reference of our object (Integer with value 46) to i2
// 11) We try to remove in s the same reference as i2 contained
// But it's possible with the equals method
In Java, variables are always passed to methods by value.
When you say a.remove(i2), you pass the *value* of i2 to the remove method. What is the value of i2? Is it 46? No. It's a reference to an Integer which has the value 46.
When we change i2 like this: i2 = 47; what we really do is we create a new instance of Integer, with the value 47, and we assign a reference to that object to i2.
Let's call remove. When you call remove, you pass the reference to the Integer to it. The method will then call the Integer's hashCode() method to determine where in the HashSet it is located. It will see that 47 does not map to a bucket in the set, so nothing happens.