HashCodes and HashMaps

R

Roedy Green

Think about the HashMap inside serialisation writes. It has to keep
track of which objects it has already put into the stream and the
offset where it put them.

You would think it would work with a simple HashMap using the objects
themselves as keys and an Integer or Long offset object.

But the catch is the objects may have equals methods that are not
equivalent to == anymore. That equals could conflate objects that
were different.

I suppose there are two ways it could be implemented -- cannibalise
the code from HashMap using == instead of equals, or using little
pointer objects that point to the object and whose equals method
compares references.
 
M

Murray

Roedy Green said:
Think about the HashMap inside serialisation writes. It has to keep
track of which objects it has already put into the stream and the
offset where it put them.

You would think it would work with a simple HashMap using the objects
themselves as keys and an Integer or Long offset object.

But the catch is the objects may have equals methods that are not
equivalent to == anymore. That equals could conflate objects that
were different.

I suppose there are two ways it could be implemented -- cannibalise
the code from HashMap using == instead of equals, or using little
pointer objects that point to the object and whose equals method
compares references.

Would IdentityHashMap do the job?
 
R

Roedy Green

Would IdentityHashMap do the job?

That looks exactly like my hypothetical cannibalised HashMap
It would also use the original Object.hashCode, not the overridden
one, with System.identityHashCode(Object x)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top