• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Map remove method

 
Ranch Hand
Posts: 647
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 .
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
remove() is on optional part of the Map interface. Perhaps the implementor does not support it?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then the method should throw an UnsupportedOperationException:

UnsupportedOperationException - if the remove operation is not supported by this map

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 647
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic