WeakHashMap - 它的目的是什么以及如何正确使用它

发布于 2024-11-16 10:58:29 字数 711 浏览 5 评论 0原文

今天我发现这篇博文讨论了用法缓存上的 WeakHashMap 。令人感兴趣的是,键不是值而是被存储为弱引用,当引用不再有效时,整个键值对就会从 WeakHashMap 中删除。因此,这将导致发生以下情况:

WeakHashMap map = new WeakHashMap();
SomeClass myReference1 = .... 
map.put(new Long(10), myReference1);
// do some stuff, but keep the myReference1 variable around!
SomeClass myReference2 = map.get(new Long(10)); // query the cache
if (myReference2 == null) {
    // this is likely to happen because the reference to the first new Long(10) object
    // might have been garbage-collected at this point
}

我很好奇什么情况下会利用 WeakHashMap 类?

Today I found this blog post which discussed usages of WeakHashMap over cache. It was intrigued by the fact that not the values, but the keys are stored as weak references, and when the reference is no more alive, the entire key-value pair is removed from the WeakHashMap. This would therefore cause the following to happen:

WeakHashMap map = new WeakHashMap();
SomeClass myReference1 = .... 
map.put(new Long(10), myReference1);
// do some stuff, but keep the myReference1 variable around!
SomeClass myReference2 = map.get(new Long(10)); // query the cache
if (myReference2 == null) {
    // this is likely to happen because the reference to the first new Long(10) object
    // might have been garbage-collected at this point
}

I am curious what scenarios then would take advantage of the WeakHashMap class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半寸时光 2024-11-23 10:58:29

当您想要将元数据附加到您无法控制其生命周期的对象时。一个常见的例子是类加载器,但必须注意避免创建值->键引用循环。

When you want to attach metadata to an object for which you don't control the lifecycle. A common example is ClassLoader, though care must be taken to avoid creating a value->key reference cycle.

烟燃烟灭 2024-11-23 10:58:29

有很多用途,但一个真正重要的用途是当您想通过 Class 键入某些内容时。维护对 Class 实例的强引用可以固定整个类加载器。

顺便说一句,Guava 有一套更完整的非强引用映射结构。

There are many uses, but one really important one is when you want to key something by Class. Maintaining a strong reference to Class instances can peg entire classloaders.

As an aside, Guava has a much more complete set of non-strong reference mapping constructs.

天邊彩虹 2024-11-23 10:58:29

我运行示例代码来了解 HashMap 和 WeakHashMap 之间的区别,希望它有所帮助

        Map hashMap= new HashMap();
        Map weakHashMap = new WeakHashMap();

        String keyHashMap = new String("keyHashMap");
        String keyWeakHashMap = new String("keyWeakHashMap");

        hashMap.put(keyHashMap, "helloHash");
        weakHashMap.put(keyWeakHashMap, "helloWeakHash");
        System.out.println("Before: hash map value:"+hashMap.get("keyHashMap")+" and weak hash map value:"+weakHashMap.get("keyWeakHashMap"));

        keyHashMap = null;
        keyWeakHashMap = null;

        System.gc();  

        System.out.println("After: hash map value:"+hashMap.get("keyHashMap")+" and weak hash map value:"+weakHashMap.get("keyWeakHashMap"));

输出将是:

Before: hash map value:helloHash and weak hash map value:helloWeakHash
After: hash map value:helloHash and weak hash map value:null

I ran the sample code to understand the difference between HashMap and WeakHashMap, Hope it helps

        Map hashMap= new HashMap();
        Map weakHashMap = new WeakHashMap();

        String keyHashMap = new String("keyHashMap");
        String keyWeakHashMap = new String("keyWeakHashMap");

        hashMap.put(keyHashMap, "helloHash");
        weakHashMap.put(keyWeakHashMap, "helloWeakHash");
        System.out.println("Before: hash map value:"+hashMap.get("keyHashMap")+" and weak hash map value:"+weakHashMap.get("keyWeakHashMap"));

        keyHashMap = null;
        keyWeakHashMap = null;

        System.gc();  

        System.out.println("After: hash map value:"+hashMap.get("keyHashMap")+" and weak hash map value:"+weakHashMap.get("keyWeakHashMap"));

The output will be:

Before: hash map value:helloHash and weak hash map value:helloWeakHash
After: hash map value:helloHash and weak hash map value:null
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文