:)))))
cool!
but I did implement the hash without relying on the Xid ..
thanks man, that's the spirit!
marc
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]]On Behalf Of Kabutz, Heinz
> Sent: Friday, August 11, 2000 1:43 AM
> To: 'jBoss Developer'
> Subject: RE: [jBoss-Dev] [newbie] byte[].hashcode()
>
>
> public final class ByteArrayWrapper
> {
> private final byte[] contents;
> public ByteArrayWrapper(byte[] contents)
> {
> this.contents = contents;
> }
>
> public byte[] getContents()
> {
> return contents;
> }
>
> public int hashCode()
> {
> int result = 0;
> int pos = 0;
> for (int i=contents.length-1; i>=0; i--)
> {
> result += (contents[i] << pos);
> pos = (pos + 8) % 32;
> }
> return result;
> }
>
> // this is 10x as slow as the other hashCode
> public int hashCodeSlow()
> {
> StringBuffer buf = new StringBuffer(contents.length*3);
> for (int i=contents.length-1; i>=0; i--)
> {
> buf.append(contents[i]);
> }
> return buf.toString().hashCode();
> }
>
> public boolean equals(Object obj)
> {
> try
> {
> ByteArrayWrapper other = (ByteArrayWrapper)obj;
> if (contents.length != other.contents.length) return false;
> for (int i=contents.length-1; i>=0; i--)
> {
> if (contents[i] != other.contents[i]) return false;
> }
> return true;
> }
> catch(NullPointerException ex)
> {
> return false;
> }
> catch(ClassCastException ex)
> {
> return false;
> }
> }
>
> public static void main(String[] args)
> {
> ByteArrayWrapper baw1 = new ByteArrayWrapper(
> new byte[] {1, 2, 3, 5, 1, 77, 125, 10, 11});
> ByteArrayWrapper baw2 = new ByteArrayWrapper(
> new byte[] {1, 2, 3, 5, 1, 77, 125, 10, 11});
> if (baw1.equals(baw2)) System.out.println("The two arrays are equal");
> if (baw2.equals(baw1)) System.out.println("The two arrays are equal");
> System.out.println("baw1.hashCode()=" + baw1.hashCode());
> System.out.println("baw2.hashCode()=" + baw2.hashCode());
> final int NUMBER_HASHCODE_TESTS = 100000;
> long start = System.currentTimeMillis();
> for (int i=0; i<NUMBER_HASHCODE_TESTS; i++)
> {
> baw1.hashCode();
> }
> System.out.println("It took " + (System.currentTimeMillis()-start) +
> "ms to call hashCode " + NUMBER_HASHCODE_TESTS + " times");
> }
> }
>
> -----Original Message-----
> From: Rickard �berg [mailto:[EMAIL PROTECTED]]
> Sent: 11 August 2000 06:59
> To: jBoss Developer
> Subject: Re: [jBoss-Dev] [newbie] byte[].hashcode()
>
>
> Hey
>
> marc fleury wrote:
> > did you know that
> >
> > byte[] byte1 = {1,2,3};
> > byte[] byte2 = {1,2,3};
> >
> > yields
> > byte1.equals(byte2) = false;
> > and
> > byte1.hashCode() != byte2.hashCode();
>
> Of course.. since you are comparing the arrays, and not their contents.
> Both equals and hashCode will be the Object defaults, which work on
> memory adress of the actual object.
>
> > blows my mind man....
> >
> > what is the best way to compute a byte[].hashCode()?
>
> Iterate and add the elements together. Also depends on the distribution
> of the contents. Why do you need a byte array BTW? Is this for the Xid?
> Why not use an enumerated long or something?
>
> /Rickard
>
> --
> Rickard �berg
>
> Email: [EMAIL PROTECTED]
> http://www.telkel.com
> http://www.jboss.org
> http://www.dreambean.com
>
>