如何在Java中为每个对象获取唯一ID? [英] how do I get a unique ID per object in Java?

查看:3726
本文介绍了如何在Java中为每个对象获取唯一ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个向量集,以避免使用迭代器分配和类似的
来破坏GC(对于每个遍历HashSet值的集合引用和set迭代器,每个都获得一个new / free)密钥)

I made a vector set in order to avoid thrashing the GC with iterator allocations and the like ( you get a new/free each for both the set reference and the set iterator for each traversal of a HashSet's values or keys )

无论如何, Object.hashCode()方法是每个对象的唯一ID。 (对于64位版本会失败吗?)

anyway supposedly the Object.hashCode() method is a unique id per object. (would fail for a 64 bit version?)

但无论如何它都是可覆盖的,因此不能保证唯一,也不能保证每个对象实例的唯一性。

But in any case it is overridable and therefore not guaranteed unique, nor unique per object instance.

如果我想创建一个ObjectSet,我如何为每个对象实例获得一个保证唯一ID?

If I want to create an "ObjectSet" how do I get a guaranteed unique ID for each instance of an object??

I刚发现这个:它回答了它。

I just found this: which answers it.

如何获取覆盖hashCode()的对象的唯一ID?

推荐答案

java.lang.System.identityHashCode(obj); 会为你做这件事,如果你真的需要它并理解其后果。它获取了身份哈希码,即使提供哈希码的方法已被覆盖。

java.lang.System.identityHashCode(obj); will do this for you, if you really need it and understand the repercussions. It gets the identity hashcode, even if the method to provide the hashcode has been overridden.

这篇关于如何在Java中为每个对象获取唯一ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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