File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes change the value in a Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "change the value in a Hashmap" Watch "change the value in a Hashmap" New topic
Author

change the value in a Hashmap

Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
The discussion we have been having on HashMaps has been very interesting.
I had one question-
In the given HashMap,
{"NY", Vector 1}
{"SF", Vector 2}
If I change some contents of Vector, say change the spelling of some string, how do I reflect it on the HashMap?
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Lucky Singh:
In the given HashMap,
{"NY", Vector 1}
{"SF", Vector 2}
If I change some contents of Vector, say change the spelling of some string, how do I reflect it on the HashMap?

If you change a value in a vector, and that vector is referenced by your map as above, you'll see the change in the map as well, because it doesn't make a copy of the vector. You just have 2 (or more) references to a single object, so the change to the object is 'seen' by all the references. Or are you talking about something else?
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
{"NY, ["New York", "1852"]}
{"SF",["San Francisco", "1900"]}
HashMap has key and value where value is a vector.
I want to do the following-
{"NY", ["New York", "1852"]}
{"SF", ["San Francisco", "2001"]}
How do I change the value in the vector and in turn HashMap?
The new HashMap must have the new values.
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Lucky Singh:
{"NY, ["New York", "1852"]}
{"SF",["San Francisco", "1900"]}
HashMap has key and value where value is a vector.
I want to do the following-
{"NY", ["New York", "1852"]}
{"SF", ["San Francisco", "2001"]}
How do I change the value in the vector and in turn HashMap?
The new HashMap must have the new values.

Get and update the Vector if you don't already have a reference to it:
Vector sf = (Vector)myMap.get("SF");
or let's assume you added the vector 'sf' to the map previously. Either way:
sf.set(1, "2001");
Not a problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: change the value in a Hashmap
 
Similar Threads
core java
what's the difference between hashtable and hashMap
Comparing 2 vectors, am I efficent?
ArrayList and Vector
Vector Vs. Hashtable