这是我的用例,我有一个逻辑上等于我的HashMap键但不是同一个对象(不是==)的对象。 我需要从HashMap中获取实际键对象,以便我可以在其上进行同步。 我知道我可以遍历ketSet,但与散列相比,这是缓慢的。
通过java.util.HashMap实现,我看到了一个正是我需要的getEntry(Object key)方法。 知道为什么没有曝光吗?
你能想到我能把钥匙拿出来的其他任何方式吗?
Here is my use case, I have an object that is logically equal to my HashMap key but not the same object (not ==). I need to get the actuall key object out of the HashMap so that i can synchronise on it. I am aware that i can iterate over the ketSet, but this is slow in comparison to hashing.
Looking through the java.util.HashMap implementation i see a getEntry(Object key) method that is exactly what i need. Any idea why this has not been exposed?
Can you think of any other way i can get the key out?
最满意答案
我认为你最好在值上添加一个额外的间接层。 关键也应该是一个“纯粹”的价值。 代替:
Map<ReferenceObjectKey,Thing> map;使用:
Map<ValueObjectKey,ReferenceObject<Thing>> map;I think you would be better off putting in an extra layer of indirection on the value. The key should also be a "pure" value. Instead of:
Map<ReferenceObjectKey,Thing> map;Use:
Map<ValueObjectKey,ReferenceObject<Thing>> map;更多推荐
发布评论