The String class uses the following algorithm to compute the hash code:

How String Hashcode value is calculated?

int hash = 0; 
for(int i = 0; i < length(); i++) {
    hash = 31 * hash + charAt(i); 
}
Copy the code

For strings, the hash codes are derived from their contents.

The default hashCode method in the Object class derives the hash code from the object’s memory address.

java.lang.Object

  • int hashCode()

should be used when you want the hash of a single object, and will throw an exception if the object is null.

java.util.Objects

  • static int hash(Object... objects)

should be used in cases when you want a hash of a sequence of objects, e.g. when defining your own hashCode method and want a simply-coded hash for multiple values that make up the identity of your object.

  • static int hashCode(Object a)

should be used when you want the hash of a single object, without throwing if the object is null.

java.util.Arrays

static int hashCode(xxx[] a)

Reference

Objects.hash() vs Objects.hashCode(), clarification needed