user489041
user489041

Reputation: 28294

Hashing in Java

I have a Long and a String. I want to create a hash of both of those object. Meaning, I want some function that will take arbitrary number of objects and return me one hash value. Does such a function exits?

Something like this:


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

Upvotes: 3

Views: 347

Answers (2)

codelark
codelark

Reputation: 12334

The Apache Commons HashCodeBuilder has a reflection-based invocation that is similar to what you want.

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

Upvotes: 6

Mark Peters
Mark Peters

Reputation: 81054

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

It doesn't accept varargs, but you can wrap it with your own varargs library function if you wish:

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

Upvotes: 11

Related Questions