I have to write a hash function, under the following two conditions:
- I don't know anything about
Object o
that is passed to the method - it can be a String, and Integer, or an actual custom object; - I am not allowed to call
hashCode()
at all.
Approach that I am using now, to calculate the hash code:
- Write object to the byte stream;
- Convert byte stream to the byte array;
Loop through the byte array and calculate hash by doing something like this:
hash = hash * PRIME + byteArray[i]
My question is it a passable approach and is there a way to improve it? Personally I feel like the scope for this function is too broad - there is no information about what the objects are, but I have little say in this situation.