求高手。HashMap插入的时候值不同。输出的时候为什么一样?呜呜。。
HashMap插入的时候值不同。输出的时候为什么一样呢?求解。。
materialsCache:
maId
1
2
3
equipmentCache:
eqId maIdList
1 1,2,3
2 2,3
3 1,3
public void setMaterialEquId(){
ConcurrentHashMap<Integer, Materail> materailIds1 = new ConcurrentHashMap<Integer, Materail>();
Materail newMaterail = new Materail();
List<Integer> materailList = (ArrayList<Integer>)materialsCache.getKeys();
List<Integer> equipmentList = (ArrayList<Integer>)equipmentCache.getKeys();
String[] materailId = null;
List<Integer> equipmentIdList = null;
for(int i=1;i<=materailList.size();i++){
Materail materail = (Materail)materialsCache.get(i);
System.out.println("。。。。。。。。。。。。。。。。。。。。" + materail.getId());
equipmentIdList = new ArrayList<Integer>();
for(int j=1;j<=equipmentList.size();j++){
Equipment equipment = (Equipment)equipmentCache.get(j);
System.out.println(equipment.getId() + " " +equipment.getMaterialIdList());
materailId = equipment.getMaterialIdList().split(",");
for(int a = 0; a < materailId.length; a ++){
if(materail.getId() == Integer.parseInt(materailId[a])){
System.out.println("materail.getId()===========" + materail.getId());
System.out.println("Integer.parseInt(materailId[i])===========" + Integer.parseInt(materailId[a]));
equipmentIdList.add(equipment.getId());
break;
}
}
}
newMaterail.setId(materail.getId());
newMaterail.setEquipmentIdList(equipmentIdList);
materailIds1.put(newMaterail.getId(), newMaterail);
System.out.println(materailIds1.get(newMaterail.getId()).getId()+" "+materailIds1.get(newMaterail.getId()).getEquipmentIdList());
}
System.out.println();
System.out.println();
System.out.println();
materailList = (ArrayList<Integer>)materialsCache.getKeys();
for(int g=1;g<=materailIds1.size();g++){
System.out.println(g + " " + materailIds1.get(g).getId() + " " + materailIds1.get(g).getEquipmentIdList());
}
}
后台输出结果是:
。。。。。。。。。。。。。。。。。。。。1
1 1,2,3
materail.getId()===========1
Integer.parseInt(materailId[i])===========1
2 2,3
3 1,3
materail.getId()===========1
Integer.parseInt(materailId[i])===========1
1===========[1, 3]
。。。。。。。。。。。。。。。。。。。。2
1 1,2,3
materail.getId()===========2
Integer.parseInt(materailId[i])===========2
2 2,3
materail.getId()===========2
Integer.parseInt(materailId[i])===========2
3 1,3
2===========[1, 2]
。。。。。。。。。。。。。。。。。。。。3
1 1,2,3
materail.getId()===========3
Integer.parseInt(materailId[i])===========3
2 2,3
materail.getId()===========3
Integer.parseInt(materailId[i])===========3
3 1,3
materail.getId()===========3
Integer.parseInt(materailId[i])===========3
3===========[1, 2, 3]
1 3 [1, 2, 3]
2 3 [1, 2, 3]
3 3 [1, 2, 3]
正确的应该是:
1 1 [1,3]
2 2 [1,2]
3 3 [1,2,3]
materialsCache:
maId
1
2
3
equipmentCache:
eqId maIdList
1 1,2,3
2 2,3
3 1,3
public void setMaterialEquId(){
ConcurrentHashMap<Integer, Materail> materailIds1 = new ConcurrentHashMap<Integer, Materail>();
Materail newMaterail = new Materail();
List<Integer> materailList = (ArrayList<Integer>)materialsCache.getKeys();
List<Integer> equipmentList = (ArrayList<Integer>)equipmentCache.getKeys();
String[] materailId = null;
List<Integer> equipmentIdList = null;
for(int i=1;i<=materailList.size();i++){
Materail materail = (Materail)materialsCache.get(i);
System.out.println("。。。。。。。。。。。。。。。。。。。。" + materail.getId());
equipmentIdList = new ArrayList<Integer>();
for(int j=1;j<=equipmentList.size();j++){
Equipment equipment = (Equipment)equipmentCache.get(j);
System.out.println(equipment.getId() + " " +equipment.getMaterialIdList());
materailId = equipment.getMaterialIdList().split(",");
for(int a = 0; a < materailId.length; a ++){
if(materail.getId() == Integer.parseInt(materailId[a])){
System.out.println("materail.getId()===========" + materail.getId());
System.out.println("Integer.parseInt(materailId[i])===========" + Integer.parseInt(materailId[a]));
equipmentIdList.add(equipment.getId());
break;
}
}
}
newMaterail.setId(materail.getId());
newMaterail.setEquipmentIdList(equipmentIdList);
materailIds1.put(newMaterail.getId(), newMaterail);
System.out.println(materailIds1.get(newMaterail.getId()).getId()+" "+materailIds1.get(newMaterail.getId()).getEquipmentIdList());
}
System.out.println();
System.out.println();
System.out.println();
materailList = (ArrayList<Integer>)materialsCache.getKeys();
for(int g=1;g<=materailIds1.size();g++){
System.out.println(g + " " + materailIds1.get(g).getId() + " " + materailIds1.get(g).getEquipmentIdList());
}
}
后台输出结果是:
。。。。。。。。。。。。。。。。。。。。1
1 1,2,3
materail.getId()===========1
Integer.parseInt(materailId[i])===========1
2 2,3
3 1,3
materail.getId()===========1
Integer.parseInt(materailId[i])===========1
1===========[1, 3]
。。。。。。。。。。。。。。。。。。。。2
1 1,2,3
materail.getId()===========2
Integer.parseInt(materailId[i])===========2
2 2,3
materail.getId()===========2
Integer.parseInt(materailId[i])===========2
3 1,3
2===========[1, 2]
。。。。。。。。。。。。。。。。。。。。3
1 1,2,3
materail.getId()===========3
Integer.parseInt(materailId[i])===========3
2 2,3
materail.getId()===========3
Integer.parseInt(materailId[i])===========3
3 1,3
materail.getId()===========3
Integer.parseInt(materailId[i])===========3
3===========[1, 2, 3]
1 3 [1, 2, 3]
2 3 [1, 2, 3]
3 3 [1, 2, 3]
正确的应该是:
1 1 [1,3]
2 2 [1,2]
3 3 [1,2,3]
作者: cao_xu1988 发布时间: 2011-06-16
为什么没有人回答。呜呜。快哭了。!
作者: cao_xu1988 发布时间: 2011-06-16
你不结贴当然不想回答
作者: yewuqing007 发布时间: 2011-06-16
给你说下吧
看你结贴率,不想看你代码猜的
放进map的对象是用的同一个地址,后面的数据覆盖了前面的
每次new个新的
看你结贴率,不想看你代码猜的
放进map的对象是用的同一个地址,后面的数据覆盖了前面的
每次new个新的
作者: yewuqing007 发布时间: 2011-06-16
引用 3 楼 yewuqing007 的回复:
给你说下吧
看你结贴率,不想看你代码猜的
放进map的对象是用的同一个地址,后面的数据覆盖了前面的
每次new个新的
给你说下吧
看你结贴率,不想看你代码猜的
放进map的对象是用的同一个地址,后面的数据覆盖了前面的
每次new个新的
+1
作者: alexandertech 发布时间: 2011-06-16