How HashMap
internally differentiate between null
and 0
as key.
As per this post the hash code for null
key is 0
, Is it correct?
If yes then both should go in same bucket at index 0
and there should be one value in the HashMap
but this is not how HashMap
works.
Can somebody explain me? what is the hash code for null
key in HashMap
?
Sample code:
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