为什么getEntry(Object key)没有在HashMap上公开?(Why is getEntry(Object key) not exposed on HashMap?)

编程入门 行业动态 更新时间:2024-10-28 05:24:54
为什么getEntry(Object key)没有在HashMap上公开?(Why is getEntry(Object key) not exposed on HashMap?)

这是我的用例,我有一个逻辑上等于我的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;

更多推荐

本文发布于:2023-08-03 15:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Object   getEntry   key   exposed   HashMap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!