从列表中读取信息并放入地图

时间:2013-08-03 00:36:25

标签: java

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

1 个答案:

答案 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;
}