Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 928
Mike Avatar asked Mar 27 '16 19:03

Mike


1 Answers

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.

like image 148
Andy Thomas Avatar answered Oct 18 '22 21:10

Andy Thomas