According to the comment of this post , hascode of null objects can throw NPE or a value of zero . This is a specific implementation. but within the same implementation, why Objects.hashcode and hascode(instance) return different values. for ex:
public class EqualsTesting { public static void main(String[] args){ String p1 =null; String p2 = null; System.out.println(Objects.hashCode(p1)); System.out.println(p2.hashCode()); } }
Output:
0 Exception in thread "main" java.lang.NullPointerException at BinaryTrees.EqualsTesting.main(EqualsTesting.java:14)
If so, this will not affect the key look-up in the HashMap , where null Key-value pairs allowed. (This can be hash to bucket 0 or throw a NPE )
java object hashcode hashmap
eagertoLearn
source share