Ken said:
Hi. In a HashMap, is there a way to replace a value associated with a
particular key. IOW, if I have a Map pair ("Ken", 40), is there a way
to change the 40 to a 41 without having to delete and replace the
entire pair?
In general, just do a put("Ken", new Integer(41)) -- or leave out the
wrapper stuff with the 1.5 beta. If you mean that you've already
retrieved the value and want to replace it in constant time, then you'd
need to redesign the Map to use a mutable value class that you've
written yourself.
Unlike with keys, you *can* safely mutate the value stored in a Map,
even in a way that affects equals and hashCode. Nevertheless, it's
generally best to avoid allowing any mutations that affect equals and
hashCode in any of your classes, unless you really have to. There are
unfortunate situations where this is needed, such as if you're
implementing a mutable collection that implements something from the
Collections API, where the interface actually specifies poor behavior
for equals and hashCode.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation