Convert your int's to a String (with each int separated by a non-numeric
character like "|"), then, compute a 32-bit CRC on the String.

Code example:

public int hashCode( ) {
        StringBuffer strBuff = new StringBuffer( );
        strBuff.append(primary_id);
        strBuff.append('|');
        strBuff.append(secondary_id);
        String str = strBuff.toString();
        int hashCode = str.hashCode( );
        return hashCode;
}

Great article on this technique:

http://www.ejbnow.com/ejbtips/ejbtip_1.html

----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, January 23, 2001 1:49 AM
Subject: Best way to implement a hash code for PK class?


EJB spec says that PK class has to return a hashcode. What is the best way
to implement this? My problem is, hashCode has to
be int, and my PK is a compound key of three ints! I often saw:

(PK1 + PK2 + PK3).hashCode()

but PK1+PK2+PK3 can throw overflow (maxint+maxint+maxint>maxint!).

So what would be the best solution?


----
To unsubscribe, send email to [EMAIL PROTECTED] and
include in the body of the message "unsubscribe jonas-users".
For general help, send email to [EMAIL PROTECTED] and
include in the body of the message "help".

Reply via email to