ConcurrentHashMap与弱键和身份哈希? [英] ConcurrentHashMap with weak keys and identity hash?

查看:288
本文介绍了ConcurrentHashMap与弱键和身份哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中使用弱键和身份哈希获取 ConcurrentHashMap ?我认为谷歌收藏可以给这样的事情,但我可以从标准库中得到它吗?

How do I get a ConcurrentHashMap with weak keys and identity hashes in Java? I think Google Guava Collections can give such a thing, but can I get it from the standard library? What other options do I have?

推荐答案


我认为Google Guava Collections可以提供这样的功能,但是我可以从标准库中获得它吗?

I think Google Guava Collections can give such a thing, but can I get it from the standard library?

简单的答案是否定的。Java SE没有实现这个特定的组合。

The short answer to that is No. Java SE does not implement this particular combination.


  • 您可以实例化一个 java.util.concurrent.ConcurrentHashMap WeakReference 键,并执行一些额外的工作来实现删除破坏的引用的映射条目,但不会给你身份哈希语义。

  • You could instantiate a java.util.concurrent.ConcurrentHashMap with WeakReference keys, and do some extra work to implement removal of map entries for broken references, but that won't give you identity hash semantics.

您可以使用 WeakReference 键实例化 java.util.IdentityHashMap

使用 java.util.WeakHashMap 不会给你并发或身份哈希。

Using a java.util.WeakHashMap won't give you either concurrency or identity hashing.

类在覆盖自然 equals hashcode 方法。

You could (in theory) wrap the key class in something that overrode the natural equals and hashcode methods. But that is most likely to be unusable.

我认为不可能通过覆盖中的方法来实现, ConcurrentHashMap IdentityHashMap

I don't think it would be possible to do this by overriding methods in either ConcurrentHashMap or IdentityHashMap.

也许唯一可行的选择是更改键类 equals hashcode 方法。但是这不适用于内置键类型(特别是 final )或在应用程序其他部分需要基于值的equals / hashcode的情况。

Maybe the only viable option would be to change the key classes equals and hashcode methods to be identity based. But that won't work for "built in" key types (especially final ones) or for cases where you need value-based equals/hashcode in other parts of the application.

这篇关于ConcurrentHashMap与弱键和身份哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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