I am debugging the above code. Looks like even if the key is there in _theCache, it's not getting removed. _theCache shows same size even after it goes through _theCache.remove(currentItem);
line of code.
How does the Map remove works? why it doesn't seem to work in above case.
I am debugging the above code. Looks like even if the key is there in _theCache, it's not getting removed. _theCache shows same size even after it goes through _theCache.remove(currentItem);
line of code.
How does the Map remove works? why it doesn't seem to work in above case.
thanks
Trupti
Hard to say what the problem is with just this code, but here is my train of thought:
You are trying to remove the key differently than you are checking if it contains the key, which is the proper method for converting currentItem to the proper key value? What data type is currentItem? What type is used for the keys in the map? How do you know it is the remove() that isn't working, and not the containsKey()?
Yeah, I suspect currentItem is not getting boxed into a Long, but into something else. What type is it, and what are then type parameters (if any) of the Map?
Ernest Friedman-Hill wrote:Yeah, I suspect currentItem is not getting boxed into a Long, but into something else. What type is it, and what are then type parameters (if any) of the Map?
Yes you are correct. Before removing the key, I need to cast it to Long.
Let me guess: currentItem is an int. That won't be boxed into a Long but into an Integer, and then it's quite logical that the key cannot be found while trying to remove.
As a side note: since Java 5.0 you should use Long.valueOf, Integer.valueOf etc instead of new Long, new Integer etc. Those are just a tad more efficient for small values, and for large values they do exactly the same.