本文介绍了为什么getEntry(Object key)不公开在HashMap上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的用例,我有一个逻辑上等于我的HashMap键但不是相同对象(不是==)的对象。我需要从HashMap中取出实体关键对象,以便我可以同步它。我知道我可以遍历ketSet,但与散列相比,这是缓慢的。

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.

通过java.util.HashMap实现,我看到一个getEntry(Object关键)方法,这正是我需要的。任何想法,为什么这没有被暴露?

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?

推荐答案

我认为你最好在值上添加额外的间接层。关键还应该是一个纯粹的价值。而不是:

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;

使用:

Map<ValueObjectKey,ReferenceObject<Thing>> map;

这篇关于为什么getEntry(Object key)不公开在HashMap上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:26