hash() and hashCode()

The hash values returned by the methods hash() or hashCode() are typically used as a key for storing the object in a hash-using collection, such as HashSet(). The default implementation in the  Object superclass is based on the object reference in memory.  It returns different hash values for two objects of the same class with the same values of the instance fields. That is why, if you need two class instances to have the same hash value for the same state, it is important to override the default hashCode() implementation using one of these methods:

Please notice that these two methods return different hash values for the same object when it is used as a single-element input array of the method Objects.hash():

System.out.println(Objects.hash("s1"));           //prints: 3645
System.out.println(Objects.hashCode("s1")); //prints: 3614

The only value that yields the same hash from both methods is null:

System.out.println(Objects.hash(null));      //prints: 0
System.out.println(Objects.hashCode(null)); //prints: 0
When used as a single not-null parameter, the same value has different hash values returned from the methods Objects.hashCode(Object value) and Objects.hash(Object... values). The value null yields the same hash value, 0, returned from each of these methods.

Another advantage of using the class Objects for hash value calculation is that it tolerates null values, while the attempt to call the instance method hashCode() on the null reference generates NullPointerException.