在Java中合并2个HashMaps

时间:2012-05-23 18:36:12

标签: java android dictionary hashmap

我有一个需要合并两个HashMap的程序。哈希映射的密钥为String,值为Integer。合并的特殊条件是,如果密钥已经在字典中,则Integer需要添加到现有值而不是替换它。以下是我到目前为止抛出NullPointerException

的代码
public void addDictionary(HashMap<String, Integer> incomingDictionary) {
        for (String key : incomingDictionary.keySet()) {
            if (totalDictionary.containsKey(key)) {
                Integer newValue = incomingDictionary.get(key) + totalDictionary.get(key);
                totalDictionary.put(key, newValue);
            } else {
                totalDictionary.put(key, incomingDictionary.get(key));
            }
        }
    }

3 个答案:

答案 0 :(得分:2)

如果您的代码无法保证incomingDictionary在到达此方法之前将被初始化,则必须进行空检查,无法解决

public void addDictionary(HashMap<String, Integer> incomingDictionary) {
    if (incomingDictionary == null) {
        return; // or throw runtime exception
    }
    if (totalDictionary == null) {
        return;// or throw runtime exception
    }
    if (totalDictionary.isEmpty()) {
        totalDictionary.putAll(incomingDictionary);
    } else {
        for (Entry<String, Integer> incomingIter : incomingDictionary.entrySet()) {
            String incomingKey = incomingIter.getKey();
            Integer incomingValue = incomingIter.getValue();
            Integer totalValue = totalDictionary.get(incomingKey);
            // If total dictionary contains null for the incoming key it is
            // as good as replacing it with incoming value.
            Integer sum = (totalValue == null ? 
                                            incomingValue : incomingValue == null ? 
                                                    totalValue : totalValue + incomingValue
                          );
            totalDictionary.put(incomingKey, sum);
        }
    }
}

考虑到HashMap允许null作为值,代码中容易出现NPE的另一个地方是

Integer newValue = incomingDictionary.get(key) + totalDictionary.get(key);

如果这两个中的任何一个为空,你将获得NPE。

答案 1 :(得分:2)

你可能有一个未初始化的词典。 这是一个解决方案:

public void addDictionary(HashMap<String, Integer> incomingDictionary) {
    if (incomingDictionary == null) {
        throw new IllegalArgumentException("incomingDictionary cannot be null.");
    }
    if (totalDictionary == null) {
        throw new IllegalArgumentException("totalDictionary cannot be null.");
        // or another solution:
        // totalDictionary = new HashMap<String, Integer>();
        // totalDictionary.putAll(incomingDictionary);
        // return;
    }

    for (Map.Entry<String, Integer> entry : incomingDictionary.entrySet()) {
        Integer oldValue = totalDictionary.get(entry.getKey());
        if (oldValue != null){
            // here entry.getValue() could be null!
            // Never put a null value in your Map, or add a test here
            Integer newValue = entry.getValue() + oldValue;
            totalDictionary.put(entry.getKey(), newValue);
        } else {
            totalDictionary.put(entry.getKey(), entry.getValue());
        }
    }
}

答案 2 :(得分:0)

考虑到totalDictionary已正确初始化,请在:

Integer newValue = incomingDictionary.get(key) + totalDictionary.get(key);

totalDictionary.get(key)无法返回null。您可能需要在此之前添加以下内容:

if(totalDictionary.get(key) == null)
  totalDictionary.put(key, 0);