2つをマージする必要があるプログラムがありますHashMap
。ハッシュマップには、aであるキーと。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));
}
}
}