什么是 WeakHashMap 以及何时使用它? [英] What is a WeakHashMap and when to use it?

查看:44
本文介绍了什么是 WeakHashMap 以及何时使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是WeakHashMap,什么时候应该使用它?WeakHashMapHashMap 有什么区别?

What is a WeakHashMap and when should one be using it? What are the differences between a WeakHashMap and a HashMap?

推荐答案

如果没有其他对键对象的强引用,弱哈希图中的元素可以被垃圾收集器回收,这使得它们可用于缓存/查找存储.

Elements in a weak hashmap can be reclaimed by the garbage collector if there are no other strong references to the key object, this makes them useful for caches/lookup storage.

弱引用不限于这些哈希表,您可以对单个对象使用弱引用.它们对于节省资源很有用,您可以保留对某些内容的引用,但在没有其他内容引用它时允许收集它.(顺便说一句,强引用是普通的 java 引用).还有一些弱引用往往不像软引用那样容易收集(在最后一个强引用消失后,它不会长时间挂起)

Weak reference are not restricted to these hash tables, you can use WeakReference for single objects. They are useful to save resource, you can keep a reference to something but allow it to be collected when nothing else references it. (BTW, a strong reference is a normal java reference). There are also weak references which tend not to be as readily collected as soft references (which don't tend to hang about for long after the last strong reference disappears)

这篇关于什么是 WeakHashMap 以及何时使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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