CQM
CQM

Reputation: 44278

Java, convert object to softreference

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

Upvotes: 3

Views: 467

Answers (2)

Roger Lindsj&#246;
Roger Lindsj&#246;

Reputation: 11553

I think this is what you are looking for

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

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.

Upvotes: 3

Steven Schlansker
Steven Schlansker

Reputation: 38536

Take a look at Guava's MapMaker class, which allows you to specify soft values. Way easier than reimplementing it yourself.

Upvotes: 0

Related Questions