放置哈希映射值arrayincorrect

时间:2018-01-24 09:25:21

标签: java json hashmap

我已将值json数组放入哈希映射,但最后一个值是正确的。我仍然不清楚使用哈希映射,列表和数组列表。我的代码:

Map<String, Object> departmentPHSSuportEmail = new HashMap<String, Object>();
Map<String, Object> subSourceMap = null;

List<Map<String , Object>> myMap  = new ArrayList<Map<String,Object>>();

subSourceMap = new HashMap<>();
subSourceMap.put("name", "bobby");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");

subSourceMap.put("name", "2sadasd");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");      

subSourceMap.put("name", "ggfgf");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");      

myMap.add(0, subSourceMap);
myMap.add(1, subSourceMap);
myMap.add(2, subSourceMap);


Map<String, Object> attachment = new HashMap<String, Object>();
attachment.put(ApplicationConstanta.MsExchange.TYPE, "");
attachment.put(ApplicationConstanta.MsExchange.VALUE, "");
departmentPHSSuportEmail.put(ApplicationConstanta.EmailForwardString.ATTACHMENT, attachment);

Map<String, Object> subSource = new HashMap<String, Object>();
subSource.put(ApplicationConstanta.MsExchange.TYPE, ApplicationConstanta.MsExchange.STRING);
subSource.put(ApplicationConstanta.MsExchange.VALUE, myMap);
departmentPHSSuportEmail.put(ApplicationConstanta.EmailForwardString.SUB_SOURCE, subSource);

System.out.println("JSON : " + JsonUtil.toJson(departmentPHSSuportEmail));

结果:

JSON : {
  "Attachment" : {
    "type" : "",
    "value" : ""
  },
  "SubSource" : {
    "type" : "string",
    "value" : [ {
      "address" : "xxx.xxxx@xxx.co.id",
      "name" : "ggfgf"
    }, {
      "address" : "xxx.xxxx@xxx.co.id",
      "name" : "ggfgf"
    }, {
      "address" : "xxx.xxxx@xxx.co.id",
      "name" : "ggfgf"
    } ]
  }
}

为什么值&#34;值&#34;对象都一样吗?我把不同的价值。如何解决这个问题呢?感谢

1 个答案:

答案 0 :(得分:0)

您看到此问题是因为您重复使用相同的Hashmap对象,该对象基本上会覆盖您的数据。让我们一步一步地完成这个步骤:

List<Map<String , Object>> myMap  = new ArrayList<Map<String,Object>>();

subSourceMap = new HashMap<>();
subSourceMap.put("name", "bobby");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");

现在您的subSourceMap包含:

name: bobby
address: xxx.xxxx@xxx.co.id

然后你做

subSourceMap.put("name", "2sadasd");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");  

使用此代码修改现有的subSourceMap并覆盖其内容。现在您的subSourceMap包含:

name: 2sadasd
address: xxx.xxxx@xxx.co.id

然后你做:

subSourceMap.put("name", "ggfgf");
subSourceMap.put("address", "xxx.xxxx@xxx.co.id");

您将再次修改现有的subSourceMap。现在您的subSourceMap包含:

name: ggfgf
address: xxx.xxxx@xxx.co.id

最后你做了:

myMap.add(0, subSourceMap);
myMap.add(1, subSourceMap);
myMap.add(2, subSourceMap);

这会将对同一subSourceMap的三个引用放入列表中。它不会复制地图,只是添加对同一对象的引用。因此,当您现在将此列表转换为JSON时,您自然会看到所看到的内容。

要解决此问题,您需要为每个条目创建一个新的地图:

List<Map<String , Object>> myMap  = new ArrayList<Map<String,Object>>();

// create a fresh map
Map<String,Object> subSourceMap1 = new HashMap<>();
subSourceMap1.put("name", "bobby");
subSourceMap1.put("address", "xxx.xxxx@xxx.co.id");

// create a fresh map
Map<String,Object> subSourceMap2 = new HashMap<>();
subSourceMap2.put("name", "2sadasd");
subSourceMap2.put("address", "xxx.xxxx@xxx.co.id");      

// create a fresh map
Map<String,Object> subSourceMap3 = new HashMap<>();
subSourceMap3.put("name", "ggfgf");
subSourceMap3.put("address", "xxx.xxxx@xxx.co.id");      

// note that you do not need to specify the index here, just use
// add with 1 parameter which will append to the end of the list
myMap.add(subSourceMap1);
myMap.add(subSourceMap2);
myMap.add(subSourceMap3);

您可以阅读本文关于Java引用的文章,其中显示了一个类似的示例,以便更熟悉Java中的对象引用如何工作:http://www.informit.com/articles/article.aspx?p=174371&seqNum=4