Java,将对象转换为软引用
我需要将一个数据对象放入包含软引用的weakhashmap中。如何将“Drawable”对象转换为软引用?
WeakHashMap <String, SoftReference<Drawable>> tempPulled = new WeakHashMap<String, SoftReference<Drawable>>();
Drawable pulled = BitmapDrawable.createFromResourceStream(null, null, conn.getInputStream(), "galleryImage", options);
SoftReference<Drawable> sPulled;
tempPulled.put(id, pulled);
tempPulled 应该放置 "sPulled" ,软引用
I need to put a data object into my weakhashmap containing softreferences. How do I convert my "Drawable" object into a softreference?
WeakHashMap <String, SoftReference<Drawable>> tempPulled = new WeakHashMap<String, SoftReference<Drawable>>();
Drawable pulled = BitmapDrawable.createFromResourceStream(null, null, conn.getInputStream(), "galleryImage", options);
SoftReference<Drawable> sPulled;
tempPulled.put(id, pulled);
tempPulled should be putting "sPulled" , the softreference
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这就是您正在寻找的
但是您可能想要添加一个队列以在引用的对象被删除时从映射中删除软引用(或者您的缓存将随着键和空软引用而增长。
I think this is what you are looking for
But you probably want to add a queue to remove the SoftRefence from the map should the referenced object be removed (or your cache will grow with keys and empty softrefences.
查看 Guava 的 MapMaker 类,允许您指定软值。比自己重新实现要容易得多。
Take a look at Guava's MapMaker class, which allows you to specify soft values. Way easier than reimplementing it yourself.