是否有机会在没有hashTable的情况下从哈希码访问对象? [英] Is there any chance of accessing object from hashcode without hashTable in java?

查看:108
本文介绍了是否有机会在没有hashTable的情况下从哈希码访问对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Object obj1。现在我将hashCode()存储在变量中。现在我只想通过哈希码访问obj1。


意味着得到该对象的引用。有没有办法这样做!!!

解决方案

Hastables [ ^ ]只是一种收集形式。它就像一个关联数组。它不会替换引用,但它使用引用。使用哈希码确实为对象生成一种索引,但是对象本身通过它的引用链接到结构中。如果没有对对象保持引用,垃圾收集器将销毁它。因此,如果您丢失所有引用,则无法再访问该对象。哈希码只是对象的计算属性(由方法返回),仅此而已。



所以,很快:不。



但为什么你认为你需要这个?

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!!!

解决方案

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?


这篇关于是否有机会在没有hashTable的情况下从哈希码访问对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆