Java hashing

I have Longand a String. I want to create a hash of both of these objects. Meaning, I want some function to execute an arbitrary number of objects and return to me a single value of the hash function. Does such a function come out?

Something like that:


public int getHash(Object... objects)
{
     //somehow returns a hash of all these objects
}

+3
source share
2 answers

Take a look at Arrays.hashCode(Object[]).

It does not accept varargs, but you can wrap it with your own varargs library function if you want:

public static int computeHashCode(Object... objects) {
   return Arrays.hashCode(objects);
}
+11
source

Apache Commons HashCodeBuilder contains a reflection-based call that looks like what you want.

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

Source: https://habr.com/ru/post/1795846/


All Articles