我正在编写一个代码,其中有一个嵌套的hashmap,一切正常,同时将child添加到parent,不同键的所有值都被最后一个键的值覆盖。 请尽快提出一些解决方案。先谢谢,下面是代码。
Map<String, Object> provData = new HashMap<String, Object>();
Map<Integer, Map<String, Object>> provNetAndNetLoc = new HashMap<Integer, Map<String, Object>>();
try {
for (Integer provNetCycleId : provNetInfo) {
this.provNetLocPs = jdbcShell.getJdbcConnection().prepareStatement(this.provNetLocQuery);
this.provNetLocPs.setString(1, tin);
this.provNetLocPs.setInt(2, provNetCycleId);
this.provNetLocRs = this.provNetLocPs.executeQuery();
while(this.provNetLocRs.next()){
provData.put("netCycleStartDate", provNetLocRs.getDate("netCycleStartDate"));
provData.put("netCycleEndDate", provNetLocRs.getDate("netCycleEndDate"));
provData.put("netLocCycleId", provNetLocRs.getInt("netLocCycleId"));
provData.put("netLocCycStartDate", provNetLocRs.getDate("netLocCycStartDate"));
provData.put("netLocCycleEndDate", provNetLocRs.getDate("netLocCycleEndDate"));
provData.put("locId", provNetLocRs.getInt("locId"));
provData.put("locStartDate", provNetLocRs.getDate("locStartDate"));
provData.put("locEndDate", provNetLocRs.getDate("locEndDate"));
provData.put("provId", provNetLocRs.getInt("provId"));
System.out.println("HashCode "+provData.hashCode());
provNetAndNetLoc.put(provNetCycleId, provData);
}
}
}
我得到的输出就像:
在第一次迭代中
{85={locEndDate=4000-01-01, locStartDate=2000-01-01, netCycleStartDate=2014-03-01, provId=87, netLocCycStartDate=2014-03-11, netLocCycleId=67, netLocCycleEndDate=4000-01-01, locId=106, netCycleEndDate=4000-01-01}}
非迭代
{85={locEndDate=4000-01-01, locStartDate=2014-03-15, netCycleStartDate=2013-03-01, provId=122, netLocCycStartDate=2014-03-01, netLocCycleId=70, netLocCycleEndDate=4000-01-01, locId=208, netCycleEndDate=4000-01-01},
88={locEndDate=4000-01-01, locStartDate=2014-03-15, netCycleStartDate=2013-03-01, provId=122, netLocCycStartDate=2014-03-01, netLocCycleId=70, netLocCycleEndDate=4000-01-01, locId=208, netCycleEndDate=4000-01-01}}
答案 0 :(得分:0)
当你将provData放入provNextAndNetLoc或者在循环的每次迭代中创建一个新的provData时,你需要复制provData。例如:
for (Integer provNetCycleId : provNetInfo) {
this.provNetLocPs = jdbcShell.getJdbcConnection().prepareStatement(this.provNetLocQuery);
this.provNetLocPs.setString(1, tin);
this.provNetLocPs.setInt(2, provNetCycleId);
this.provNetLocRs = this.provNetLocPs.executeQuery();
while(this.provNetLocRs.next()){
// because I create the map in the inner loop, I'll be loading each
// result into a new map. When it was declared outside the loop, we
// kept referencing the same map instance and thus were overwriting it
Map<String, Object> provData = new HashMap<String, Object>();
provData.put("netCycleStartDate", provNetLocRs.getDate("netCycleStartDate"));
provData.put("netCycleEndDate", provNetLocRs.getDate("netCycleEndDate"));
provData.put("netLocCycleId", provNetLocRs.getInt("netLocCycleId"));
provData.put("netLocCycStartDate", provNetLocRs.getDate("netLocCycStartDate"));
provData.put("netLocCycleEndDate", provNetLocRs.getDate("netLocCycleEndDate"));
provData.put("locId", provNetLocRs.getInt("locId"));
provData.put("locStartDate", provNetLocRs.getDate("locStartDate"));
provData.put("locEndDate", provNetLocRs.getDate("locEndDate"));
provData.put("provId", provNetLocRs.getInt("provId"));
System.out.println("HashCode "+provData.hashCode());
provNetAndNetLoc.put(provNetCycleId, provData);
}
答案 1 :(得分:0)
问题是你不能为每个周期创建一个新的provData-Map。因此,您在每次迭代期间始终更改Map的相同实例,并将其放在provNetAndNetLoc中。
我建议你在每个周期的provData中创建一个新实例。
while(this.provNetLocRs.next()){
provData = new HashMap<String, Object>();