Search code examples
javajava-8guavahashcode

Guava.Objects.hashCode vs Java.Objects.hashCode


In Java 8 there is a class java.util.Objects, which contains hashCode() method. At the same time Google Guava 19 contains com.google.common.base.Objects, which also has hashCode() method.

My questions:

  1. Is there any reason why should I prefer Guava's 19 hashCode() over Java's 8?

  2. Can I completely rely on Java 8 hashCode() or it's better to stay with Guava?


Solution

  • 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.