Hashmap替换值

时间:2017-05-09 08:07:56

标签: java

我不确定hashmap内容是如何变化的。

以下是我的代码:

public static void main(String args[]){

    HashMap<String, Double> map= new HashMap<>();
    map.put("com.x",30.00d);
    map.put("com.y",70.00d);
    map.put("com.z",70.00d);

    List<String> readyServers=new ArrayList<>();
    readyServers.add("system-01");
    readyServers.add("system-02");

    returnServerMapping(2,170.00d,map,readyServers);
}



private static void returnServerMapping(int serverRequired, double totalExpectedExecutionTime,
                                                                   HashMap<String, Double> map, List<String> readyServers) {
    Map<String,List<String>> testSuiteAndServerMap= new HashMap<>();
    List<String> suitesForServer = new ArrayList<>();

    Double executionTimePerServer = totalExpectedExecutionTime / serverRequired;

    int currentServerNumber = 1;
    Double currentTime = 0.0d;
    for (Map.Entry<String, Double> entry : map.entrySet()) {
        Double t = entry.getValue();

        System.out.println(currentServerNumber + " of " + serverRequired + ": " + entry.getKey());
        suitesForServer.add(entry.getKey());
        System.out.println("1.suites:"+suitesForServer);

        currentTime += t;

        if(currentServerNumber == serverRequired){// In the last case, let's get the all remaining suites & finally map this list with the last server
        }else if(currentTime >= executionTimePerServer && currentServerNumber < serverRequired){
            System.out.println("Putting data into the map : server:"+readyServers.get(currentServerNumber-1)+"="+suitesForServer);
            System.out.println("2.suites:"+suitesForServer);
            testSuiteAndServerMap.put(readyServers.get(currentServerNumber-1), suitesForServer);
            suitesForServer.removeAll(suitesForServer);
            System.out.println("3.suites:"+suitesForServer);
            currentServerNumber++;currentTime = 0.0d;
        }
    }
    System.out.println("4.suites:"+suitesForServer);
    testSuiteAndServerMap.put(readyServers.get(serverRequired-1), suitesForServer);
    System.out.println("Final servers & suites:"+testSuiteAndServerMap);
}

,输出

1 of 2: com.z
1.suites:[com.z]
1 of 2: com.y
1.suites:[com.z, com.y]
Putting data into the map : server:system-01=[com.z, com.y]
2.suites:[com.z, com.y]
3.suites:[]
2 of 2: com.x
1.suites:[com.x]
4.suites:[com.x]
Final servers & suites:{system-01=[com.x], system-02=[com.x]}

所以问题是当system-01 = [com.z,com.y]时,为什么最后将它设置为system-01 = [com.x]?

编辑:调试后,我找到了hashmap内容 在suitesForServer.removeAll(suitesForServer)之前; =&GT; key-system-01&amp;值0-com.z,值1-com.y

在suitesForServer.removeAll(suitesForServer)之后; =&GT; key-system-01&amp;值大小= 0

已解决:HashMap指的是列表。一旦列表内容发生变化(在任何地方),hashmap也会修改内容。为了解决这个问题,我使用了一个只复制了列表内容的浅拷贝。

List<String> suites= new ArrayList<>(suitesForServer);
                testSuiteAndServerMap.put(readyServers.get(currentServerNumber-1), suites); 

1 个答案:

答案 0 :(得分:1)

地图的值始终是相同的列表实例。 将列表放入else地图后,您可以通过suitesForServer.removeAll(suitesForServer)清除地图。 我建议您在if中将com.x添加到列表中。

在每次迭代中,您应该创建一个新的List实例:

for (Map.Entry<String, Double> entry : map.entrySet()) {
    List<String> suitesForServer = new ArrayList<>();
    ...