我有一个字符串到整数的映射。我想检查 map 是否具有特定字符串,如果是,则修改它映射到的整数值。
Map <String, Integer> m= new SortedMap <String,Integer>();
Map <String, Integer> m2 = new SortedMap<StringInteger>();
//do some stuff
Iterator <String,Integer> i = m2.iterator();
//add some values into the first map first map
while (i.hasNext()){
String temp = i.next();
int found = m.get(temp);
if ( found != null) {//this is giving me a syntax error , something about how ints
can't be null , do I just compare it to zero
//process value that temp maps to
averages.put(temp, val); //
}
}
此外,当我在第二个循环期间放入 key 时,它会删除第一个 key ,并放入另一个具有新过程值的 key 。
最佳答案
您需要将int
更改为Integer
:
Integer found = m.get(temp);
'int' 是一个基元,不能与 null 进行比较。
map 键是唯一的,因此如果您输入相同的键两次,它将被替换
关于java - 如何使用map函数get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11598567/