Thursday, December 29, 2005

Reference Objects and hashcode

In my previous post, i had brought up the observation that Reference objects cannot be used as reliable keys in Maps/Sets. Intutively I had expected the Reference objects to delegate the equals/hashCode methods to its referrant object.But it was not so. It left me thinking why was it designed not to delegate.

The top reason that comes to my mind is that
if the referent object that the Reference points to is GC'ed, then if we are delegating, then equals/hashCode would have to be handled by Reference.Typically it would have do something like

public int hashCode(){
  if(get() == null) return 0 or -1;
  return get().hashCode();

This means that in a map, where keys are references, all the keys(references) whose referents
have been GC'ed, would end up having the same hashCode. This would cause weird runtime issues when the map is being resized and we would have no clue why ! Same hold true when we try to add ReferenceObjects to a Set.

We might also wonder why not then store the hashCode of the referent in a separate variable when creating the reference. Well hashCode can change over the lifetime of an object, meaning we will never know if the stored value is still valid. If it does not change, it will work.

Geez, i've been breaking my head trying to create something like a ConcurrentWeakHashMap and nothing seems to work. So I just made a call and decided my keys have to be objects whose hashCode will not change with data. Makes life more simpler. If at all I need a true ConcurrentWeakHashMap i've decided to use the less performant method of creating a WeakHashmap and doing a Collections.synchronizedMap() !

Subscribe to comments for this post


Derek DeMarco said...

Anything else of interest regarding the ConcurrentWeakHashMap? I have the same desire, and am hitting the same walls. I have a different approach that I'd be willing to discuss off thread, but its not perfect either. Anyway, hope you see this :) aim logicaldope

Anonymous said...

Hey guys!

I know some folks here who have gotten results with Male-Extra penis enhancement pills. Im just about to order as i've been hearing amazing results with it. But first off, I want to know if Male Extra does it work as good as they say it does?

I know 1-2 inches Penis growth is realistic and easily accomplished. But I'd like REAL FAST penis growth in the 5 inches range. Dont laugh, I want to have the best penis growth possible because my new girlfriend dated a friggin porn stud!

Guys here know what im talking about?

I make that I NEED to know the best way to enlarge Penis. Male-extra pills promises 3 inches growth within several weeks or they return your money. So I guess that essentially means, if my penis dont grow real fast? They lose money, coz I got to use the pills at the same time get my money back right?

Does this seem like a scam?

Please reply at the soonest possible time!
I need to make a decision fast!

Im already sold on it to be honest, but I just need that one last push so I dont put this off any longer.

The advertisement for the pill I last saw at this site. I hope its still there.

K guys thanks. Hope to hear your feedback alright? Bye :D

Clicky Web Analytics