map在list循环里面赋值问题

查询回一个List<Map<String,Object>>集合,for循环里面遍历这个集合给新的list复制判断到特定的条件给这个list添加map
结果,List集合中的所有当前map都被处理了,各种new对象,都未解决;

问题原因是:指向问题:修改对象指向的是一个对象地址

解决办法:循环list时,将对象每次都克隆一次,putAll();

List<Map<String, Object>> allCountList = new ArrayList<Map<String, Object>>();
Map<String, Object> objectMap = new HashMap<String, Object>();
objectMap.put(“weizhi”, “”);
objectMap.put(“dw”, “艾欧尼亚”);
objectMap.put(“renshu”, “”);
allCountList.add(objectMap);
// Map<String, Object> objectMap1 = new HashMap<String, Object>();
// objectMap1.put(“dydj”, “”);
// objectMap1.put(“dw”, “昌吉”);
// objectMap1.put(“bdzsl”, “”);
// allCountList.add(objectMap1);
List<Map<String, Object>> newAllCountList = new ArrayList<Map<String, Object>>();
// for (Map<String, Object> objectMap2 : allCountList
// ) {
Map<String, Object> objectMap1 = allCountList.get(0);
if (objectMap1.get(“weizhi”).toString().equals("")) {
Map<String, Object> objectMap2 = objectMap1;
Map<String, Object> objectMap3 = objectMap1;
objectMap1.put(“weizhi”, “sup”);
objectMap1.put(“renshu”, 15);
newAllCountList.add(objectMap1);
System.out.println(“1-------” + newAllCountList);
// Map<String, Object> objectMap1 = objectMap2;
objectMap2.put(“weizhi”, “ad”);
objectMap2.put(“renshu”, 53);
newAllCountList.add(objectMap2);
System.out.println(“2-------” + newAllCountList);
objectMap3.put(“weizhi”, “mid”);
objectMap3.put(“renshu”, 20);
newAllCountList.add(objectMap2);
System.out.println(“3-------” + newAllCountList);
} else {
newAllCountList.add(objectMap1);
}

原先这样的代码输出为
[{weizhi=mid, dw=艾欧尼亚, renshu=20}, {weizhi=mid, dw=艾欧尼亚, renshu=20}, {weizhi=mid, dw=艾欧尼亚, renshu=20}]
需要改为

List<Map<String, Object>> allCountList = new ArrayList<Map<String, Object>>();
Map<String, Object> objectMap = new HashMap<String, Object>();
objectMap.put(“weizhi”, “”);
objectMap.put(“dw”, “艾欧尼亚”);
objectMap.put(“renshu”, “”);
allCountList.add(objectMap);
// Map<String, Object> objectMap1 = new HashMap<String, Object>();
// objectMap1.put(“dydj”, “”);
// objectMap1.put(“dw”, “昌吉”);
// objectMap1.put(“bdzsl”, “”);
// allCountList.add(objectMap1);
List<Map<String, Object>> newAllCountList = new ArrayList<Map<String, Object>>();
// for (Map<String, Object> objectMap2 : allCountList
// ) {
Map<String, Object> objectMap1 = allCountList.get(0);
if (objectMap1.get(“weizhi”).toString().equals("")) {
Map<String, Object> objectMap2 = new HashMap<String, Object>();
objectMap2.putAll(objectMap1);//这行代码解决指向问题
Map<String, Object> objectMap3 = new HashMap<String, Object>();
objectMap3.putAll(objectMap1);
objectMap1.put(“weizhi”, “sup”);
objectMap1.put(“renshu”, 15);
newAllCountList.add(objectMap1);
System.out.println(“1-------” + newAllCountList);
// Map<String, Object> objectMap1 = objectMap2;
objectMap2.put(“weizhi”, “ad”);
objectMap2.put(“renshu”, 53);
newAllCountList.add(objectMap2);
System.out.println(“2-------” + newAllCountList);
objectMap3.put(“weizhi”, “mid”);
objectMap3.put(“renshu”, 20);
newAllCountList.add(objectMap2);
System.out.println(“3-------” + newAllCountList);
} else {
newAllCountList.add(objectMap1);
}

输出为
[{weizhi=sup, dw=艾欧尼亚, renshu=15}, {weizhi=ad, dw=艾欧尼亚, renshu=53}, {weizhi=ad, dw=艾欧尼亚, renshu=53}]

解决0.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值