2

I have a following scenario:

 public class MapTest {
 String name = "guru";
 /**
 * @param args
 */
 public static void main(String[] args) {
 MapTest mapTest = new MapTest();
 Map map = new HashMap();
 map.put("name", mapTest.name);
 System.out.println(mapTest.name);
 map.put("name", "raj");
 System.out.println(mapTest.name);
 }
 }

output is:

guru
guru

is there any way that I can get the output as

guru
raj

ie. I want the HashMap map and the member variable name to in sync.

Thanks.

asked Jun 10, 2011 at 7:12

4 Answers 4

7

You can't do that. That's not the way Java works. When you write:

map.put("name", mapTest.name);

that's putting the current value of mapTest.name into the map. After the argument has been evaluated, it's completely independent of the original expression.

If you need to do something like this, you would have some sort of mutable wrapper class - you'd put a reference to the wrapper into the map, and then you can change the value within the wrapper, and it doesn't matter how you get to the wrapper, you'll still see the change.

Sample code:

import java.util.*;
class StringWrapper {
 private String value;
 StringWrapper(String value) {
 this.value = value;
 }
 public String getValue() {
 return value;
 }
 public void setValue(String value) {
 this.value = value;
 }
 @Override
 public String toString() {
 return value;
 }
}
public class Test {
 public static void main(String[] args) throws Exception {
 Map<String, StringWrapper> map = new HashMap<String, StringWrapper>();
 StringWrapper wrapper = new StringWrapper("Original");
 map.put("foo", wrapper);
 System.out.println(map.get("foo"));
 wrapper.setValue("Changed");
 System.out.println(map.get("foo"));
 }
}
answered Jun 10, 2011 at 7:15
Sign up to request clarification or add additional context in comments.

Comments

1

You appear to be confused about how maps work. For a better idea, try printing out the map.

map.put("name", mapTest.name);
System.out.println(map);
map.put("name", "raj");
System.out.println(map);

You will get:

{"name"="guru"}
{"name"="raj"}

Note that mapTest.name == "guru" always as you never modify it.

answered Jun 10, 2011 at 7:43

Comments

0

Java maps just don't support anything like this. When you do

 map.put("name", mapTest.name);

You put a reference to the object referenced by mapTest.name in the map. When you do

 map.put("name", "raj");

You put a reference to the new String object in the map. The reference to mapTest.name isn't in the map anymore.

Out of curiosity, why don't you want to just use map.get("name")?

answered Jun 10, 2011 at 7:25

Comments

0

If you want something to be performed dynamically, you should use a function/method.

public class MapTest {
 private final Map<String, String> map = new HashMap<String, String>();
 public String name() {
 return map.get("name");
 }
 public static void main(String[] args) {
 MapTest mapTest = new MapTest();
 mapTest.map.put("name", "guru");
 System.out.println(mapTest.name());
 mapTest.map.put("name", "raj");
 System.out.println(mapTest.name());
 }
}

prints

guru
raj
answered Jun 10, 2011 at 7:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.