什么是WeakHashMap和什么时候使用呢? [英] What is a WeakHashMap and when to use it?

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

问题描述

什么是 WeakHashMap 和什么时候应该使用它? WeakHashMap HashMap 之间有什么区别?

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

推荐答案

如果没有对对象的其他强引用,那么弱hashmap中的元素可以被回收,这使得它们对缓存/查找存储有用。

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

弱引用不限于这些哈希表,您可以对单个对象使用WeakReference。它们对于保存资源很有用,可以保存对某个对象的引用,但允许在没有其他引用它时收集它。 (BTW,强引用是一个普通的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天全站免登陆