在Java中合并2个哈希映射
我有一个程序需要合并两个在Java中合并2个哈希映射,java,android,dictionary,hashmap,Java,Android,Dictionary,Hashmap,我有一个程序需要合并两个HashMap。hashmaps有一个键是字符串,一个值是整数。合并的特殊条件是,如果键已经在字典中,则需要将整数添加到现有值中,而不是替换它。这是我目前为止抛出的NullPointerException的代码 public void addDictionary(HashMap<String, Integer> incomingDictionary) { for (String key : incomingDictionary.keySet()
HashMap
。hashmaps有一个键是字符串
,一个值是整数
。合并的特殊条件是,如果键已经在字典中,则需要将整数
添加到现有值中,而不是替换它。这是我目前为止抛出的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));
}
}
}
public void addDictionary(HashMap incomingDictionary){
for(字符串键:incomingDictionary.keySet()){
if(totalDictionary.containsKey(键)){
整数newValue=incomingDictionary.get(key)+totalDictionary.get(key);
totalDictionary.put(key,newValue);
}否则{
totalDictionary.put(key,incomingDictionary.get(key));
}
}
}
考虑到totalDictionary
已正确初始化,在:
Integer newValue = incomingDictionary.get(key) + totalDictionary.get(key);
totalDictionary.get(key)
无法返回null
可能您需要在前面添加类似的内容:
if(totalDictionary.get(key) == null)
totalDictionary.put(key, 0);
考虑到
totalDictionary
已正确初始化,在:
Integer newValue = incomingDictionary.get(key) + totalDictionary.get(key);
totalDictionary.get(key)
无法返回null
可能您需要在前面添加类似的内容:
if(totalDictionary.get(key) == null)
totalDictionary.put(key, 0);
如果您的代码不能保证
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);
}
}
}
如果这两个值中的任何一个为空,您将获得NPE。如果您的代码不能保证
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);
}
}
}
如果这两者中的任何一个为空,您将获得NPE。您可能有一个词汇表未初始化。 这里有一个解决方案:
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());
}
}
}
public void addDictionary(HashMap incomingDictionary){
if(incomingDictionary==null){
抛出新的IllegalArgumentException(“incomingDictionary不能为null”);
}
如果(totalDictionary==null){
抛出新的IllegalArgumentException(“totalDictionary不能为null”);
//或其他解决方案:
//totalDictionary=新HashMap();
//totalDictionary.putAll(incomingDictionary);
//返回;
}
对于(Map.Entry:incomingDictionary.entrySet()){
整数oldValue=totalDictionary.get(entry.getKey());
if(oldValue!=null){
//此处entry.getValue()可以为null!
//永远不要在映射中放入空值,或在此处添加测试
整数newValue=entry.getValue()+oldValue;
totalDictionary.put(entry.getKey(),newValue);
}否则{
totalDictionary.put(entry.getKey(),entry.getValue());
}
}
}
您可能有一个词典没有初始化。
这里有一个解决方案:
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());
}
}
}
public void addDictionary(HashMap incomingDictionary){
if(incomingDictionary==null){
抛出新的IllegalArgumentException(“incomingDictionary不能为null”);
}
如果(totalDictionary==null){
抛出新的IllegalArgumentException(“totalDictionary不能为null”);
//或其他解决方案:
//totalDictionary=新HashMap();
//totalDictionary.putAll(incomingDictionary);
//返回;
}
对于(Map.Entry:incomingDictionary.entrySet()){
整数oldValue=totalDictionary.get(entry.getKey());
if(oldValue!=null){
//此处entry.getValue()可以为null!
//永远不要在映射中放入空值,或在此处添加测试
整数newValue=entry.getValue()+oldValue;
totalDictionary.put(entry.getKey(),newValue);
}否则{
totalDictionary.put(entry.getKey(),entry.getValue());
}
}
}
在哪一行抛出NPE?是否初始化了totalDictionary
字段?totalDictionary在包装此函数的类的前面声明但未初始化为私有成员数据。Eclipse显示在for循环行中抛出的异常。这可能意味着incomingDictionary
为null,或者incomingDictionary
包含null
作为键。另外,您必须先初始化totalDictionary
,然后才能使用它……否则您将获得更多NPE。NPE被抛出到哪一行?您是否初始化了totalDictionary
字段?totalDictionary在包装此函数的类的前面被声明但未初始化为私有成员数据。Eclipse显示在for循环的行中抛出的异常。这可能意味着incomingDictionary
为null,或者incomingDictionary
包含null
作为键。此外,您必须在使用它之前初始化totalDictionary
,否则您将获得更多NPE。如果(totalDictionary.isEmpty())然后是totalDictionary.putAll(incomingDictionary)否则执行for循环?如果incomingDictionary为null,则putAll将抛出NullPointerException如果(totalDictionary.isEmpty())然后是totalDictionary.putAll(incomingDictionary)否则执行for循环?如果incomingDictionary为null,则putAll将抛出NullPointerExceptionIf条目。getValue()返回null,再次返回NPEInded!但我认为他的责任是永远不要将null
作为值放在映射中…但我将根据您的注释编辑我的帖子。如果映射中的`getValue(key)中没有包含key的值`,它可以返回null;如果entry.getValue()返回null,则再次NPEInded!但我认为他的责任是永远不要将null
作为值放在映射中…但我将根据您的评论编辑我的帖子。如果映射不包含`getValue(key)`中的键值,它可以返回null;