Common hash codes in Java

I am a little new to Java. Is there a hashcode utility class that can generate a good hash code for a shared object with members of arbitrary types inside the class? Can I recursively go through an instance of a shared java object and copy a hash code that ensures that two identical objects of this class will always calculate the same hash code? The context of this question is Android, and Objects.hashCode (), etc. are not available to me in the used Java version (and I have no way to upgrade to a newer version). I guess the answer is not based on what I have seen so far.

+4
source share
4 answers

You can try HashCodeBuilderfrom Apache Commons Lang. It is able to generate a hash code that reflects as follows:

public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this);
}
+4
source

With Java 7 you have Objects.hash ()

return Objects.hash(this.foo, this.bar, this.baz);

It also helps implement equals:

return Objects.equals(this.foo, o.foo)
       && Objects.equals(this.bar, o.baz)
       && Objects.equals(this.baz, o.baz)
+10
source

Apache Commons-Lang HashCodeBuilder. hashCode, , (, - , , Java Bean), - , .

+1

Guava . Hasher

0

All Articles