开发者

Need help with HashMaps?

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);
     开发者_Python百科   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.


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"));
    }
}


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.


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")?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜