【问题标题】:Java, convert object to softreferenceJava,将对象转换为软引用
【发布时间】:2012-01-17 14:31:54
【问题描述】:

我需要将一个数据对象放入包含软引用的弱​​哈希图中。如何将“可绘制”对象转换为软引用?

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" ,即软引用

【问题讨论】:

    标签: java memory-management soft-references weakhashmap


    【解决方案1】:

    我想这就是你要找的东西

    SoftReference<Drawable> sPulled = new SoftReference<Drawable>(pulled);            
    

    但是如果引用的对象被删除,您可能希望添加一个队列以从映射中删除 SoftRefence(或者您的缓存将随着键和空软引用而增长。

    【讨论】:

      【解决方案2】:

      查看Guava's MapMaker 类,它允许您指定软值。比自己重新实现要容易得多。

      【讨论】:

      • 我正在使用内置类。不重新发明任何东西
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 2012-08-01
      • 2012-08-29
      • 2012-05-29
      • 2016-05-14
      • 2020-09-01
      • 1970-01-01
      相关资源
      最近更新 更多