Strange HashMap behavior
In my program, I have a HashMap. It has HashSets of Strings as its keys and PriorityQueues of Strings as its values. When I change the content of one of its keys, it is no longer remain as a member of the HashMap. This seems strange to me, since I do not change the reference of the key. I just change its content. Please take a look at the following snippet:
Basically, I expect to see two "Yes!"s, indeed, it prints only one. I did a thorough debugging and realized that the reference number for myHashSet doesn't change after removing one of it's members. So, there should be no reason for this program not to print the second "Yes!".
HashMap<HashSet<String>, PriorityQueue<String>> myHashMap=new HashMap<>();
HashSet<String> myHashSet=new HashSet<>();
PriorityQueue<String> myPriorityQueue=new PriorityQueue<>();
Any kind of help is really appreciated.
If you change the value of a key, won't that be a problem? The HashMap uses the hash of the key's content to position the value. If you want to change a the value of a key, perhaps you should remove it, change it and put it back.
For more information: print out the hashCode for myHashSet after it has been added to the HashMap,
then again after the remove call.
Then add back the item removed and print the hashcode again.
Here is what I see:
hc=406600899 <<<<< after remove
hc=406697253 <<<<< after added back