在使用HashMaps时,我发现一种奇怪的行为,
import java.util.HashMap;
public class Demo {
public static void main(String[] ar) {
HashMap<String, Integer> ht = new HashMap<>();
ht.put("1", new Integer(1));
ht.put("2", new Integer(2));
ht.put("3", new Integer(3));
System.out.println(ht.get(2));
}
}
上面的代码输出null。但是,如果我将key作为Integer 1而不是string,则将检索到值。任何人都可以解释为什么原因,为什么要获取Integer值而不是String值。
肥皂起泡泡
慕村9548890
相关分类