// PLEASE EXPLAIN FOLLOWING PROGRAM'S BEHAVIOUR ---->Please explain me how am I getting this OUTPUT ?
---->When I add i1 and i2 into LinkedHashSet with values 20 and 25 respectively
----> I am reassigning i2 as 33 and when I remove i2 it removes i2 with value 33 BUT i2 with value 25 remains in set .At this point only I have got confused.
You are assigning i2 to 33 after inserting i2 into the LinkedHashSet. Now when you try to remove i2, you are actually trying to remove a 33 from the Set which doesn't exist in the Set.
It is similar to,
hs.remove(33) // 33 doesn't exist in hs. So nothing is removed!!3
When you insert i2 into the set, the set hs stores 25 in it. So when you reassign i2 to 33, the value in hs is unchanged and is 25 only.
posted 7 years ago
Thanks for your explanation.
I read javadoc for remove method which returns Boolean value as 'false' which means value of i2 =33 has not been added to set so remove() method returns false.