How can I simulate deleting entries in WeakHashMap if there are no active links to one of its keys. I have the following code:
WeakHashMap<Integer, String> weakMap = new WeakHashMap<Integer, String>(); Integer st1 = 5; Integer st2 = 6; String val = "BB"; weakMap.put(st1, "AA"); weakMap.put(st2, val); st1 = 10;
Exit always
6 BB 5 AA
But I expect to get only 6 BB Even if I decompose the commented lines, it still produces the same output. As far as I understand, if a key in WeakHashMap does not have an active link somewhere else outside this WeakHashMap , the record with the specified key should be deleted. I'm right? If not, suggest the right solution.
source share