如何HashMap
在内部区分null
和0
是关键。
根据这篇文章null
,密钥的哈希码是0
,它是否正确?如果是,那么两者都应该在索引处位于同一个存储桶中0
,并且其中应该有一个值,HashMap
但这不是如何HashMap
工作方式。
有人能给我解释一下吗?null
输入的哈希码是什么HashMap
?
示例代码:
HashMap<Integer,String> map=new HashMap<Integer,String>();
map.put(null, "abc");
map.put(0, "xyz"); // will it override the value for null as key?
System.out.println(map.get(null)); // abc
System.out.println(map.get(0)); // xyz
Run Code Online (Sandbox Code Playgroud)