• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

change the value in a Hashmap

 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{"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
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic