Reputation: 2716
I have the below code that reads information from two lists, and puts it in a map. I have close to 500 records, but I only see 75 records in the map. Please suggest what mistake I am making :(
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;
}
}
Upvotes: 0
Views: 71
Reputation: 37813
A Map
maps one key to one value. You're overriding the value many times.
This
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;
}
is the same as
if (myStores.getStoreNumber().equals(myStores.getStoreNumber())) {
myCache.put(myStores,hardware.getHardwareInfo_North());
break;
}
If you want to combine all the lists, you could do this:
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;
}
Upvotes: 3