我有以下代码从两个列表中读取信息,并将其放在地图中。我有近500条记录,但我只在地图上看到75条记录。请提出我的错误:(
private static Map<MyStore, List<String>> buildCache() {
for (MyStore myStores : myStoreList) { //Contains a list of all the stores owned by me - has close to 600 stores.
for (HardwareInfo hardware : hardwareList) { //Contains a list infrastructure information of these stores - These lists only have store number in common.
if (myStores.getStoreNumber().equals(myStores.getStoreNumber())) {
myCache.put(myStores, hardware.getHardwareInfo_East()); //myCache is a static map.
myCache.put(myStores, hardware.getHardwareInfo_West());
myCache.put(myStores,hardware.getHardwareInfo_South());
myCache.put(myStores,hardware.getHardwareInfo_North());
break;
}
}
}
for(Map.Entry<MyStore, List<String>> entry:myCache.entrySet())
{
System.out.println("ENTRY IS: " +entry.getKey()+ " AND VALUE IS: " +entry.getValue());
}
return myCache;
}
}
答案 0 :(得分:3)
Map
将一个键映射到一个值。你多次重写这个价值。
此
if (myStores.getStoreNumber().equals(myStores.getStoreNumber())) {
myCache.put(myStores, hardware.getHardwareInfo_East()); //myCache is a static map.
myCache.put(myStores, hardware.getHardwareInfo_West());
myCache.put(myStores,hardware.getHardwareInfo_South());
myCache.put(myStores,hardware.getHardwareInfo_North());
break;
}
与
相同if (myStores.getStoreNumber().equals(myStores.getStoreNumber())) {
myCache.put(myStores,hardware.getHardwareInfo_North());
break;
}
如果要组合所有列表,可以这样做:
if (myStores.getStoreNumber().equals(myStores.getStoreNumber())) {
myCache.put(myStores, new ArrayList<String>(hardware.getHardwareInfo_East())); //myCache is a static map.
List<String> list = myCache.get(myStores);
list.addAll(hardware.getHardwareInfo_West());
list.addAll(hardware.getHardwareInfo_South());
list.addAll(hardware.getHardwareInfo_North());
break;
}