如何使用map函数获取

jim*_*myC 0 java 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); //
    }
Run Code Online (Sandbox Code Playgroud)

}

此外,当我在第二个循环中输入密钥时,它是否会删除第一个密钥,并使用新的过程值放入另一个密钥.

Tom*_*mer 5

您需要更改intInteger:

Integer found = m.get(temp);
Run Code Online (Sandbox Code Playgroud)

'int'是一个原语,不能与null比较.

地图键是唯一的,因此如果您将相同的键放两次,它将被替换