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

我有同样的问题,到目前为止对任何答案都不满意,因为他们都没有保证唯一的ID。

我也想打印用于调试的对象ID。 我知道必须有一些方法可以做到这一点,因为在Eclipse调试器中,它为每个对象指定唯一的ID。

我提出了一个解决方案,基于以下事实:如果两个对象实际上是同一个实例,则对象的“==”运算符仅返回true。

import java.util.HashMap;

import java.util.Map;

/**

* Utility for assigning a unique ID to objects and fetching objects given

* a specified ID

*/

public class ObjectIDBank {

/**Singleton instance*/

private static ObjectIDBank instance;

/**Counting value to ensure unique incrementing IDs*/

private long nextId = 1;

/** Map from ObjectEntry to the objects corresponding ID*/

private Map ids = new HashMap();

/** Map from assigned IDs to their corresponding objects */

private Map objects = new HashMap();

/**Private constructor to ensure it is only instantiated by the singleton pattern*/

private ObjectIDBank(){}

/**Fetches the singleton instance of ObjectIDBank */

public static ObjectIDBank instance() {

if(instance == null)

instance = new ObjectIDBank();

return instance;

}

/** Fetches a unique ID for the specified object. If this method is called multiple

* times with the same object, it is guaranteed to return the same value. It is also guaranteed

* to never return the same value for different object instances (until we run out of IDs that can

* be represented by a long of course)

* @param obj The object instance for which we want to fetch an ID

* @return Non zero unique ID or 0 if obj == null

*/

public long getId(Object obj) {

if(obj == null)

return 0;

ObjectEntry objEntry = new ObjectEntry(obj);

if(!ids.containsKey(objEntry)) {

ids.put(objEntry, nextId);

objects.put(nextId++, obj);

}

return ids.get(objEntry);

}

/**

* Fetches the object that has been assigned the specified ID, or null if no object is

* assigned the given id

* @param id Id of the object

* @return The corresponding object or null

*/

public Object getObject(long id) {

return objects.get(id);

}

/**

* Wrapper around an Object used as the key for the ids map. The wrapper is needed to

* ensure that the equals method only returns true if the two objects are the same instance

* and to ensure that the hash code is always the same for the same instance.

*/

private class ObjectEntry {

private Object obj;

/** Instantiates an ObjectEntry wrapper around the specified object*/

public ObjectEntry(Object obj) {

this.obj = obj;

}

/** Returns true if and only if the objects contained in this wrapper and the other

* wrapper are the exact same object (same instance, not just equivalent)*/

@Override

public boolean equals(Object other) {

return obj == ((ObjectEntry)other).obj;

}

/**

* Returns the contained object's identityHashCode. Note that identityHashCode values

* are not guaranteed to be unique from object to object, but the hash code is guaranteed to

* not change over time for a given instance of an Object.

*/

@Override

public int hashCode() {

return System.identityHashCode(obj);

}

}

}

我相信这应该确保程序生命周期内的唯一ID。 但请注意,您可能不希望在生产应用程序中使用它,因为它维护对您为其生成ID的所有对象的引用。 这意味着您创建ID的任何对象都不会被垃圾回收。

由于我将其用于调试目的,我不太关心被释放的内存。

如果需要释放内存,可以修改此项以允许清除对象或删除单个对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值