Hmm, I'm curious, Aaron what do you do that needs identityHashCode()
instead of hashCode()?
It is definitely the default hashCode() algorithm in Object, and a lot of
user-created classes will end up using it. System.identityHashCode() will
be the default hash code for any objects that do not override hashCode().
Any of those objects that go into Hashtables will be using the identity hash
code.
Oh, Stuart: one problem with the algorithm to calculate the hash code.
identityHashCode(), to the maximum extent possible, is supposed to have
different hash codes for different objects. (See Object.hashCode()). Can't
use new Random().nextInt(). There's no guarantee it will return a unique
int every time, as would be nice in hashCode. Using a class-level Random r
= new Random() and doing r.nextInt() doesn't guarantee unique ints either.
Many algorithms have a sequence that is actually > 32 bits (I think the seed
was defined as 48 bits) and is then truncated to 32, allowing the same value
to be repeated many times.
One possibility to have a a master hash code integer and increment it every
time, or better yet increment it by some odd prime number. I believe that
would would end up grabbing every number in the sequence and also get you an
even sequence of hash codes (i.e. a small hash table will be relatively
evenly divided since the codes are far apart in the number sequence). The
way to choose it: if you think that the hash tables will on average have 10
buckets, you find an odd prime number somewhere around (2^32 / 10).
--John Keiser
> -----Original Message-----
> From: Aaron M. Renn [mailto:[EMAIL PROTECTED]]
> Sent: Friday, February 05, 1999 8:34 AM
> To: Stuart Ballard; [EMAIL PROTECTED]
> Subject: Re: Hashcodes in ef
>
>
> >Replying to my own message... I've just been thinking about this a bit.
> >Assuming that the identity hash code isn't used often (probably a safe
> >assumption) why not have a list of identity hash codes and (weak)
> >references to the objects they refer to?
>
> Whether or not this is a good idea, I think Classpath makes
> extensive use of
> System.identityHashCode. In fact, I think it is the default hashCode()
> algorithm in Object. I've also used it as a default hash
> algorithm in other
> classes.
>
> --
> Aaron M. Renn ([EMAIL PROTECTED]) http://www.urbanophile.com/arenn/
>
>
>