Replace value in HashMap?

K

Ken

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?

Thanks!

Ken
 
R

Roedy Green

is there a way
to change the 40 to a 41 without having to delete and replace the
entire pair?

Have a look at the code in HashMap.put that replaces with a new value
and returns the old. The overhead is minimal, scarcely more than a
lookup.
 
C

Chris Smith

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
 
H

Hemal Pandya

Roedy Green said:
Have a look at the code in HashMap.put that replaces with a new value
and returns the old. The overhead is minimal, scarcely more than a
lookup.

This is no doubt true. But what the OP is asking for -- change the
value of a key in a map -- is exactly what Map.put does: If the map
previously contained a mapping for this key, the old value is replaced
by the specified value. See
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#put(K, V).
 
K

kk_oop

Hi. I'm replying to myself :). I just noticed in the Java API that
calling put( ) for an existing key will replace the mapped value.

Here's a follow up question that I'm not likely to answer.

If I make the key object a string, how does the HashMap know how to
determine if two keys are equal? Do I need to provide a comparator
somehow? It would be great if someone could provide an example.

Thanks!

Ken
 
R

Roedy Green

But what the OP is asking for -- change the
value of a key in a map -- is exactly what Map.put does: If the map
previously contained a mapping for this key, the old value is replaced
by the specified value. See
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#put(K, V).

We should ask OP, but I think he just wants to change the association
of a given key to a new object. That is exactly what HashMap.put does
very efficiently, much more efficiently than removing the old pair and
adding a new pair.
 
K

kk_oop

Your description of what I'm trying to figure out is correct. And all
your answers were exactly the information I was seeking.

Thanks so much!

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top