0

我有一个程序需要合并两个HashMap. 哈希映射有一个键 aString和一个值 a 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));
            }
        }
    }
4

3 回答 3

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。

于 2012-05-23T18:46:43.150 回答
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());
        }
    }
}
于 2012-05-23T18:57:45.100 回答
0

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

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

totalDictionary.get(key)无法返回null
也许你需要在之前添加这样的东西:

if(totalDictionary.get(key) == null)
  totalDictionary.put(key, 0);
于 2012-05-23T18:45:55.907 回答