Are there any general collision-free best practices to generate hash codes for any (atomic) type composite primary keys?
I thought about it for a few hours and came to the conclusion, that a string concatenated by *all* primary key columns would be the only reliable way to do so. Calling Java's hashCode method on that concatenated string should yield a unique integer. (it would in fact somehow mimic what a database index does *eyerolling*)
Code:
@Override
public int hashCode()
{
String sSurrogate = String.format("%011d", this.gameId) // 11 chars
+ String.format("%01d" , this.isHome ? 1 : 0) // 1 char
+ String.format("%011d", this.playerId) // 11 chars
+ String.format("%011d", this.rosterId) // 11 chars
+ String.format("%06d" , this.period); // 6 chars
//System.out.println("surrogate = '" + sSurrogate + "'");
return sSurrogate.hashCode();
}
However, I don't believe my solution is optimal. There must be algorithms out there that are better. The above probably runs rather slow, but it's still faster than accessing a DB generator. What about standard hashing functions like MD5, SHA1, CRC, and Adler?
Karsten
PS: This is for overriding composite key ID classes...