dcsimg
How do I update a key in HashMap ?.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   SAMUEL_GOLLAPUDI
Posted On:   Saturday, December 9, 2006 01:31 AM

How do I update a key in HashMap ?.

Re: How do I update a key in HashMap ?.

Posted By:   Anonymous  
Posted On:   Tuesday, January 2, 2007 08:20 AM

I assume you want to update the (key, value) pair to (key2, value) where key != key2.


First of all, I don't know why you want to do this, but let's assume you have a good reason.


What I would do is simply remove(key) and put(key2, value). That way your first pair is removed and the second pair added.


Some things you may want to think about when using this:


  1. If your map is ordered (e.g. LinkedHashMap), then the entry drops from its original position to the end (assuming key2 did not yet occur in the map, otherwise see the next point:)

  2. If there is already an entry for key2, its value will be overwritten by this operation.



If this is as intended, go ahead and change keys in your map; if you encountered an *oops* here, you may want to think about why you think you need this a little longer.

Re: How do I update a key in HashMap ?.

Posted By:   WarnerJan_Veldhuis  
Posted On:   Saturday, December 9, 2006 04:09 AM

You don't give much information to work with. All you can do is put a new key in the map. Make sure the newKey.equals( theOldKey) otherwise it is added instead of replaced.
About | Sitemap | Contact