I have implemented a standard LRUCache in Android that stores Objects. Each key is a unique ObjectId associated with the Object stored. My problem is that the only way to retrieve an Object from cache is by the ObjectId (no iterator). What would be the best way to implement a getAll() method? Another option would be to store all the ObjectIds in a list somewhere, so I can iterate over the lists and get all of the Objects - but what would be the best way of holding all of the ObjectIds?
Thanks!
If you're using (or extending) the LruCache
that Android provides, it has a snapshot
method that returns a map of keys (your ObjectIds) and values (your Objects). You can do something like this:
Map<ObjectIds, Object> snapshot = lruCache.snapshot();
for (ObjectIds id : snapshot.keySet()) {
Object myObject = lruCache.get(id);
}
If you're not using Android's LruCache
, then I imagine it would depend on your implementation. (I'd also be curious what motivated you to implement your own instead of subclassing the provided one!)
Using snapshot to get current collection at the moment
lruCache.snapshot().values()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With