Guava.Objects.hashCode vs Java.Objects.hashCode

To add to the accepted answer:

While Objects.hash() should be preferred to Guava in Java 7+ code, note the below (paraphrased) from Effective Java 3rd Edition by Joshua Bloch (Item 11):

Unfortunately, Objects.hash() runs more slowly because it entails

  1. array creation (*)
  2. boxing and unboxing of any primitive arguments

It is recommended for use only in situations where performance is not critical.

(*) Indeed, Objects.hash() simply invokes another static method under the hood:

public static int hash(Object... values) {
        return Arrays.hashCode(values);

What you can do to counter this is to either

  1. cache the computed hash code instead of recomputing it each time and/or
  2. lazily initialize it.

(but also remember that premature optimization is the root of many evils).


  1. Simply use your IDE to generate it for you (saves time, but boilerplate code still there). In IntelliJ: Code > Generate > equals() and hashCode()

  2. consider adding Project Lombok as a dependency

Guava's method predates' Java 7.

The Java method of the same name only accepts a single argument. But a sibling java.util.Objects.hash() accepts a variable number of arguments, like Guava's Objects.hashCode().

If you're using Java 7 or later, you can use java.util.Objects.hash(...). The Guava documentation notes this:

Note for Java 7 and later: This method should be treated as deprecated; use Objects.hash(java.lang.Object...) instead.

If you're using Java 6 or earlier, you can use Guava's method.