Hi,
I don't understand why my Map can't find the key I put into it. If I print the Map, I can see the key is present. Here is the code.
Following is the output.
run:
Current Size =1
[abc0]
Current Size =2
[abc1]
[abc0]
Current Size =3
[abc1]
[abc0]
[abc2]
Current Size =4
[abc1]
[abc0]
[abc3]
[abc2]
Current Size =5
[abc4]
[abc1]
[abc0]
[abc3]
[abc2]
Current Size =6
[abc4]
[abc1]
[abc0]
[abc5]
[abc3]
[abc2]
Current Size =7
Before size =7
After size =7
[abc4]
Before size =7
After size =7
[abc6]
Current Size =3
[abc4]
[abc7]
[abc6]
Current Size =4
[abc4]
[abc8]
[abc7]
[abc6]
Current Size =5
[abc4]
[abc9]
[abc8]
[abc7]
[abc6]
null
[[abc4], [abc9], [abc8], [abc7], [abc6]]
The problem is with this part of the output,
null
[[abc4], [abc9], [abc8], [abc7], [abc6]]
corresponding to following lines.
System.out.println(map.get("abc9".intern()));
System.out.println(map.keySet());
I am guessing it has to do something with the way I am creating my
String objects in the following method. But I don't know why is it not working correctly.
Earlier I had coded it as follows.
I had the same problem. I thought may be I need to create a new String. So I changed
to
I still had the same problem.
Then I changed the method as follows.
This worked. I was able to retrieve the value for the keys that were not reclaimed. But I can't understand why only return new String("abc" + i); works but the other way doesn't work.
Please help.
Thanks,
Chan.