0

我有以下代码从两个列表中读取信息,并将其放入地图中。我有接近 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;
}
}
4

1 回答 1

3

AMap将一个键映射到一个值。您多次覆盖该值。

这个

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;
}
于 2013-08-03T00:39:10.987 回答