Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose I have an Object obj1. Now I have its hashCode() stored in variable. Now I want to access obj1 through hashcode only.

Means get a reference of that object. Is there way of doing such way!!!
Posted

1 solution

Hastables[^] are just a form of collection. It is like an associative array. It des not replace references, but it is using references. The hashcode is used do generate a kind of index to the object, but the object itself is linked into the structure by it's reference. If no reference is kept to an object the garbage collector will destroy it. So if you loose all references, you can't access the object anymore. The hashcode is just a calculated property of an object (returned by a method), nothing more.

So, shortly: no.

But why do you think you need this?
 
Share this answer
 
Comments
28shivamsharma 26-Dec-14 10:02am    
Thanks for answer! Is there no chance to get object from any source!!!
Zoltán Zörgő 26-Dec-14 11:29am    
Again: what is the exact situation where you need this?
28shivamsharma 26-Dec-14 16:42pm    
I wanna to implement XOR linked list [http://en.wikipedia.org/wiki/XOR_linked_list] in java. Sorry for asking again.
Zoltán Zörgő 26-Dec-14 16:44pm    
And why do you need the hashcode? Why don't you use the reference itself?
... I got it, you can't xor a reference...
As you can see on that page, GC is not a friendly environment for such lists :(
Zoltán Zörgő 26-Dec-14 16:48pm    
Well, you will need unsafe code to have addresses instead of references. See this one: http://josephkirwin.com/category/xor-linked-list/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900