aspose file tools
The moose likes Java in General and the fly likes Java Map  remove method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Java Map  remove method" Watch "Java Map  remove method" New topic
Author

Java Map remove method

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 602
HI,

I have below code

if(_theCache.containsKey(new Long(currentItem)))
{
_theCache.remove(currentItem);
}

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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3041
    
    4

trupti nigam wrote:HI,

I have below code

if(_theCache.containsKey(new Long(currentItem)))
{
_theCache.remove(currentItem);
}

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()?

For a description of how .remove() works, read the API here .

Steve
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I suspect we'd need to see proof that remove() itself doesn't work (I'm pretty sure it does).

Is there any particular reason you're checking for one key, but removing based on another?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56233
    
  13

remove() is on optional part of the Map interface. Perhaps the implementor does not support it?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Then the method should throw an UnsupportedOperationException:
UnsupportedOperationException - if the remove operation is not supported by this map


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

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?


[Jess in Action][AskingGoodQuestions]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56233
    
  13

Rob Prime wrote:Then the method should throw an UnsupportedOperationException:
UnsupportedOperationException - if the remove operation is not supported by this map

"Should" being the operative word.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 602
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.

thanks
Trupti
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.
 
 
subject: Java Map remove method
 
Similar Threads
Annonymous Inner class doesnt use final var
itrating through an arraylist
Why this method behaves differently?
How to get rid of NullException Pointer?
for loop logic problem